SQL analytics templates

The samples below are the PostgreSQL forms. Open the formatter and use the template chips to load the matching MySQL, SQLite, BigQuery, or Snowflake variant where one exists.

Window function

ROW_NUMBER() and a running SUM() OVER (PARTITION BY ... ORDER BY ...) on an events table. Useful when you want sequence numbers and a cumulative amount per user.

SELECT
  user_id,
  event_date,
  event_name,
  ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_date) AS event_seq,
  SUM(amount) OVER (PARTITION BY user_id ORDER BY event_date) AS running_amount
FROM analytics.events
WHERE event_date >= DATE '2024-01-01';

Multi-CTE funnel

A WITH block that stages raw events and then session totals before the final SELECT. The extractor treats raw_events and sessions as CTE names, not base tables.

WITH raw_events AS (
  SELECT user_id, event_name, event_at
  FROM analytics.events
  WHERE event_at >= TIMESTAMP '2024-01-01'
),
sessions AS (
  SELECT user_id, MIN(event_at) AS session_start, COUNT(*) AS event_count
  FROM raw_events
  GROUP BY user_id
)
SELECT s.user_id, s.session_start, s.event_count
FROM sessions AS s
WHERE s.event_count >= 3
ORDER BY s.session_start;

Multi-table aggregation

Orders, customers, and products joined with LEFT JOIN, grouped, and filtered with HAVING. Two joins should appear in the stats strip.

SELECT
  c.country,
  p.category,
  COUNT(DISTINCT o.order_id) AS orders,
  SUM(o.amount) AS revenue
FROM orders AS o
LEFT JOIN customers AS c ON c.customer_id = o.customer_id
LEFT JOIN products AS p ON p.product_id = o.product_id
WHERE o.created_at >= DATE '2024-01-01'
GROUP BY c.country, p.category
HAVING SUM(o.amount) >= 1000
ORDER BY revenue DESC;

Deduplication

The latest row per user. BigQuery and Snowflake use QUALIFY ROW_NUMBER() = 1. PostgreSQL, MySQL, and SQLite use a subquery with ROW_NUMBER().

SELECT user_id, email, updated_at, payload
FROM (
  SELECT
    user_id,
    email,
    updated_at,
    payload,
    ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY updated_at DESC) AS rn
  FROM users
) AS ranked
WHERE rn = 1;

Time-series rollup

A daily rollup with a 7-day moving average. PostgreSQL and Snowflake use DATE_TRUNC; MySQL and SQLite use DATE(); BigQuery uses DATE_TRUNC(timestamp, DAY).

SELECT
  DATE_TRUNC('day', event_at) AS day,
  COUNT(*) AS events,
  AVG(COUNT(*)) OVER (
    ORDER BY DATE_TRUNC('day', event_at)
    ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
  ) AS ma_7d
FROM analytics.events
GROUP BY DATE_TRUNC('day', event_at)
ORDER BY day;

JSONB extraction

PostgreSQL JSONB operators payload->>'event' and (payload->'metrics'->>'ms')::numeric. Other dialects use JSON_EXTRACT, JSON_VALUE, or Snowflake colon paths.

SELECT
  id,
  payload->>'event' AS event_name,
  (payload->'metrics'->>'ms')::numeric AS duration_ms
FROM events
WHERE payload->>'event' = 'page_view'
  AND (payload->'metrics'->>'ms')::numeric > 200;

Back to the formatter

Questions

Why does the dedup template change by dialect?

QUALIFY is documented for BigQuery and Snowflake. PostgreSQL, MySQL, and SQLite get the same result from a ROW_NUMBER() subquery filtered on rn = 1.

Are the templates executed?

No. They are sample text for the formatter. This site does not connect to a warehouse.