How can we clean, deduplicate, validate, aggregate, and rank customer order data in one SQL transformation?
A 4-step CTE pipeline (deduplicated -> valid_orders -> customer_metrics -> ranked output) performs complete enterprise transformation.
Production analytics engineering, full warehouse ELT pipeline, gold layer mart generation with customer ranking.
WITH deduplicated 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 deduplicated
WHERE customer_id IS NOT NULL
AND amount IS NOT NULL
AND amount >= 0
),
customer_metrics AS (
SELECT
customer_id,
COUNT(*) AS order_count,
SUM(amount) AS total_spend
FROM valid_orders
GROUP BY customer_id
)
SELECT
customer_id,
order_count,
total_spend,
RANK() OVER (
ORDER BY total_spend DESC
) AS customer_rank
FROM customer_metrics;Practice typing production-grade SQL code for End-to-End SQL Pipeline.