Multi-Step SQL Pipelines

A common pattern in data modeling is to chain multiple SQL steps together: staging → core → datamart → assertions. Each step builds on the previous one, creating a clean transformation pipeline within a single workflow.

The Data Layer Pattern

Most production pipelines follow a three-layer approach:

source_* (raw data) → staging.* (cleaned) → core.* (modeled) → datamarts.* (aggregated)

Each layer is handled by one or more workflow steps.

Typical Pipeline Structure

Step 1: Create Filtering View (Staging)

Raw source data often contains duplicates and mass-update rows that don't carry new information. The first step creates a staging view that handles deduplication:

CREATE OR REPLACE VIEW
`{{ raw_value($project_id) }}.staging.my_table_filters`
AS

WITH
-- Remove technical duplicates
technical_deduplication AS (
  SELECT
    *,
    ROW_NUMBER() OVER (
      PARTITION BY TO_JSON_STRING(tt)
      ORDER BY db_row_timestamp DESC
    ) AS tech_ranking
  FROM `{{ raw_value($project_id) }}.source_data.raw_table` tt
),

-- Identify mass updates (rows with no actual data change)
create_row_ids AS (
  SELECT *,
    TO_HEX(MD5(
      `{{ raw_value($project_id) }}.functions.json_except`(
        TO_JSON_STRING(td),
        ['db_row_timestamp']
      )
    )) AS row_id
  FROM technical_deduplication td
  WHERE tech_ranking = 1
),

compare_row_ids AS (
  SELECT *,
    LAG(row_id) OVER (
      PARTITION BY CONCAT(id, tenant)
      ORDER BY db_row_timestamp ASC
    ) = row_id AS is_duplicate_update
  FROM create_row_ids
)

SELECT *,
  ROW_NUMBER() OVER (
    PARTITION BY CONCAT(id, tenant)
    ORDER BY db_row_timestamp DESC
  ) AS ranking
FROM compare_row_ids
WHERE is_duplicate_update IS NOT TRUE

Key techniques:

  • TO_JSON_STRING for full-row comparison
  • json_except custom function to exclude timestamps from comparison
  • LAG window function to compare consecutive versions

Step 2: Create Core Table

The core table selects from the staging view and applies naming conventions, type casting, and timezone conversions:

CREATE OR REPLACE TABLE
`{{ raw_value($project_id) }}.core.my_table_historized`
PARTITION BY TIMESTAMP_TRUNC(db_row_timestamp, MONTH)
CLUSTER BY tenant
AS

SELECT
  CONCAT(id, tenant, ranking) AS primary_key,
  CONCAT(id, tenant) AS record_id,
  tenant,

  -- Timezone conversion
  db_row_timestamp,
  TIMESTAMP(DATETIME(db_row_timestamp, "Europe/Berlin")) AS db_row_timestamp_berlin,
  created_at AS created_timestamp,
  TIMESTAMP(DATETIME(created_at, "Europe/Berlin")) AS created_timestamp_berlin,

  -- Booleans
  db_row_deleted AS is_db_row_deleted,

  -- Ranking
  ranking

FROM `{{ raw_value($project_id) }}.staging.my_table_filters`

Step 3: Create Datamart

The datamart table takes the latest version of each record (WHERE ranking = 1) and joins with supplementary data:

CREATE OR REPLACE TABLE
`{{ raw_value($project_id) }}.datamarts.my_table_unique`
PARTITION BY TIMESTAMP_TRUNC(db_row_timestamp, MONTH)
CLUSTER BY tenant
AS

SELECT
  record_id,
  tenant,
  db_row_timestamp,
  -- ... additional columns and joins
FROM `{{ raw_value($project_id) }}.core.my_table_historized`
WHERE ranking = 1

Step 4: Assertions

After the table is created, run data quality assertions to verify:

  1. Uniqueness — primary key has no duplicates
  2. Non-null — required columns are populated
  3. Freshness — latest data is from today

Enable Parameter Iteration

For steps 1–3, enable parameter iteration with the standard cross-environment parameter query. The assertion step typically runs without iteration, using raw_value($assertion_sql) to check all environments at once.

Tips

  • Use PARTITION BY TIMESTAMP_TRUNC(db_row_timestamp, MONTH) for efficient querying
  • Use CLUSTER BY tenant when data is multi-tenant
  • Always add an OPTIONS(description = "...") to document what the table contains and its primary key
  • Use table labels for metadata: labels = [('is_incremental', 'false'), ('is_external_sharing', 'true')]