dbt Model with Tests and Schema
A dbt SQL model with incremental materialization, schema tests, and source freshness checks.
-- models/marts/fct_orders.sql
{{
config(
materialized='incremental',
unique_key='order_id',
on_schema_change='sync_all_columns'
)
}}
WITH source_orders AS (
SELECT
id AS order_id,
customer_id,
amount,
status,
created_at
FROM {{ source('raw', 'orders') }}
{% if is_incremental() %}
WHERE created_at > (SELECT MAX(created_at) FROM {{ this }})
{% endif %}
),
customers AS (
SELECT * FROM {{ ref('dim_customers') }}
)
SELECT
o.order_id,
o.customer_id,
c.customer_name,
c.region,
o.amount,
o.status,
o.created_at,
CURRENT_TIMESTAMP AS loaded_at
FROM source_orders o
LEFT JOIN customers c USING (customer_id)
WHERE o.status != 'cancelled'
-- models/marts/schema.yml
-- version: 2
-- models:
-- - name: fct_orders
-- description: Fact table for completed orders
-- columns:
-- - name: order_id
-- tests: [unique, not_null]
-- - name: amount
-- tests: [not_null, positive_values]Use Cases
- Building analytics data models with dbt
- Incremental data processing in warehouses
- Data quality testing in transformation pipelines
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Spark SQL Query Example
PySpark DataFrame operations with SQL queries, window functions, and aggregations for big data.
Best for: Processing large-scale datasets with Spark
SQL Incremental Load Pattern
Incremental data load using watermark tracking to process only new and updated records efficiently.
Best for: Efficient warehouse loading without full reloads
SQL Data Deduplication Techniques
Remove duplicate records using ROW_NUMBER, DISTINCT ON, and self-join deduplication strategies.
Best for: Cleaning duplicate records in production databases
SQL Window Functions for Analytics
Advanced SQL window functions for running totals, rankings, moving averages, and gap analysis.
Best for: Building analytics dashboards with running totals