How can we build a full multi-stage pipeline that deduplicates, validates, and aggregates order data?
Chaining sequential CTEs (deduplication -> validation -> aggregation) structures end-to-end production data pipelines.
dbt production models, Spark SQL Lakehouse pipelines, enterprise data warehouse transformations.
WITH cleaned_orders AS (
SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY order_id
ORDER BY updated_at DESC
) AS rn
FROM source_orders
) ranked
WHERE rn = 1
),
valid_orders AS (
SELECT *
FROM cleaned_orders
WHERE customer_id IS NOT NULL
AND amount >= 0
)
SELECT
customer_id,
COUNT(*) AS order_count,
SUM(amount) AS total_spend
FROM valid_orders
GROUP BY customer_id;Practice typing production-grade SQL code for Production Transformation.