xiaohongshu
xiaohongshu
dbt-model-writer
Writes, edits, and creates dbt models following best practices. Use when user needs to create new dbt SQL models, update existing models, or convert raw SQL to dbt format. Handles staging, intermediate, and mart models with proper config blocks, CTEs, and documentation.
Full skill instructions
Expert dbt model creation and editing skill for analytics engineering.
Activate this skill when the user mentions:
Create new dbt models with:
Staging Models (stg_*.sql)
view{{
config(
materialized='view',
tags=['staging']
)
}}
with source as (
select * from {{ source('raw', 'orders') }}
),
renamed as (
select
order_id,
customer_id,
order_date,
status,
-- Standardize column names and types
cast(total_amount as decimal(10,2)) as total_amount,
created_at,
updated_at
from source
)
select * from renamed
Intermediate Models (int_*.sql)
ephemeral or view{{
config(
materialized='ephemeral',
tags=['intermediate']
)
}}
with orders as (
select * from {{ ref('stg_orders') }}
),
customers as (
select * from {{ ref('stg_customers') }}
),
order_items as (
select * from {{ ref('stg_order_items') }}
),
order_totals as (
select
order_id,
count(*) as item_count,
sum(quantity) as total_quantity,
sum(quantity * price) as gross_total
from order_items
group by order_id
),
joined as (
select
o.order_id,
o.customer_id,
c.customer_name,
o.order_date,
ot.item_count,
ot.total_quantity,
ot.gross_total
from orders o
join customers c on o.customer_id = c.customer_id
join order_totals ot on o.order_id = ot.order_id
)
select * from joined
Mart Models (fct_*.sql or dim_*.sql)
table or incremental{{
config(
materialized='incremental',
unique_key='order_id',
cluster_by=['order_date'],
tags=['mart', 'daily']
)
}}
with orders as (
select * from {{ ref('int_order_details') }}
)
select
order_id,
customer_id,
order_date,
item_count,
total_quantity,
gross_total,
current_timestamp() as dbt_updated_at
from orders
{% if is_incremental() %}
-- Only process new or updated orders
where order_date >= (select max(order_date) from {{ this }})
{% endif %}
Essential config parameters:
{{
config(
materialized='table', -- table|view|incremental|ephemeral
schema='analytics', -- custom schema
alias='my_table', -- custom table name
tags=['daily', 'core'], -- organizational tags
cluster_by=['date', 'id'], -- Snowflake clustering
unique_key='id', -- for incremental
on_schema_change='sync_all_columns', -- incremental schema changes
pre_hook="ALTER TABLE {{ this }} ...",
post_hook="GRANT SELECT ON {{ this }} ..."
)
}}
Append-Only (Insert New Records)
{% if is_incremental() %}
where created_at > (select max(created_at) from {{ this }})
{% endif %}
Update Existing Records (Merge)
{{
config(
materialized='incremental',
unique_key='id',
incremental_strategy='merge'
)
}}
Delete + Insert
{{
config(
materialized='incremental',
unique_key='date_day',
incremental_strategy='delete+insert'
)
}}
Always create corresponding schema.yml entries:
version: 2
models:
- name: fct_orders
description: "Fact table for order transactions"
columns:
- name: order_id
description: "Unique order identifier"
tests:
- unique
- not_null
- name: customer_id
description: "Foreign key to dim_customers"
tests:
- not_null
- relationships:
to: ref('dim_customers')
field: customer_id
- name: order_date
description: "Date order was placed"
tests:
- not_null
- name: total_amount
description: "Total order value in USD"
tests:
- not_null
Use ref() for model dependencies
select * from {{ ref('stg_orders') }}
Use source() for raw data
select * from {{ source('raw_db', 'orders') }}
Environment-specific logic
{% if target.name == 'prod' %}
-- Production-specific logic
{% else %}
-- Dev/test logic
{% endif %}
Macros for reusable logic
{{ cents_to_dollars('price_cents') }} as price_dollars
Understand Requirements
Read Existing Context
dbt_project.yml for project structureWrite the Model
Create Documentation
Validate
with deduped as (
select
*,
row_number() over (
partition by id
order by updated_at desc
) as row_num
from source
where row_num = 1
)
with current_records as (
select * from {{ ref('stg_customers') }}
),
historical_records as (
select * from {{ this }}
where is_current = true
),
changes as (
select
c.*,
h.customer_key,
case
when h.customer_key is null then true
when c.customer_name != h.customer_name then true
else false
end as has_changed
from current_records c
left join historical_records h
on c.customer_id = h.customer_id
)
-- ... SCD logic continues
{{ dbt_utils.generate_surrogate_key(['order_id', 'line_number']) }} as order_line_key
If encountering issues:
target/compiled/Before completing, verify:
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience