blog

SQL that tells you when the number is wrong

Copyable checks for freshness, volume and validity, and the reasoning behind them. Every query returns rows only when something is broken.

sql patterns

SQL week over week alert: windows and thresholds

A SQL week over week alert that stays quiet on normal dips: which windows to compare, and how to set thresholds when counts are small.

Oct 29, 2026 · 6 min read
revenue_wow.sql
SELECT
  coalesce(sum(amount) FILTER (WHERE created_at >= current_date - 7), 0) AS this_week,
  coalesce(sum(amount) FILTER (WHERE created_at <  current_date - 7), 0) AS last_week,
  round(100.0 * (
      coalesce(sum(amount) FILTER (WHERE created_at >= current_date - 7), 0)
    - coalesce(sum(amount) FILTER (WHERE created_at <  current_date - 7), 0))
    / nullif(sum(amount) FILTER (WHERE created_at < current_date - 7), 0), 1) AS pct_change
FROM orders
WHERE created_at >= current_date - 14 AND created_at < current_date;