sqladvanced
SQL Window Functions for Analytics
Advanced SQL window functions for running totals, rankings, moving averages, and gap analysis.
sqlPress ⌘/Ctrl + Shift + C to copy
-- Running total by customer
SELECT
order_id,
customer_id,
amount,
SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM orders;
-- 7-day moving average
SELECT
order_date,
daily_revenue,
AVG(daily_revenue) OVER (
ORDER BY order_date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS moving_avg_7d
FROM (
SELECT order_date, SUM(amount) AS daily_revenue
FROM orders
GROUP BY order_date
) daily;
-- Rank customers by total spend
SELECT
customer_id,
total_spend,
RANK() OVER (ORDER BY total_spend DESC) AS spend_rank,
NTILE(4) OVER (ORDER BY total_spend DESC) AS quartile
FROM (
SELECT customer_id, SUM(amount) AS total_spend
FROM orders
GROUP BY customer_id
) totals;
-- Gap analysis: days between orders
SELECT
customer_id,
order_date,
LAG(order_date) OVER (
PARTITION BY customer_id ORDER BY order_date
) AS prev_order_date,
order_date - LAG(order_date) OVER (
PARTITION BY customer_id ORDER BY order_date
) AS days_between_orders
FROM orders;
-- Percent of total within each category
SELECT
category,
product,
revenue,
ROUND(100.0 * revenue / SUM(revenue) OVER (PARTITION BY category), 2) AS pct_of_category
FROM product_sales;Use Cases
- Building analytics dashboards with running totals
- Customer behavior analysis with gap detection
- Revenue reporting with rankings and percentiles
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
sqladvanced
SQL Window Functions for Analytics
Use window functions for running totals, rankings, moving averages, and gap detection in analytics.
Best for: Building cumulative revenue dashboards
#sql#window-functions
sqlintermediate
SQL Running Totals and Cumulative Metrics
Calculate running totals, cumulative counts, and percent-of-total using window functions and partitions.
Best for: Building cumulative revenue dashboards
#sql#window-functions
pythonadvanced
PySpark Window Functions
Use PySpark window functions for running totals, rank, lag/lead, and percentile computations.
Best for: sales analytics
#pyspark#spark
pythonbeginner
DuckDB In-Memory Analytics
Run fast analytical SQL on pandas DataFrames or Parquet files without a server using DuckDB.
Best for: serverless analytics
#duckdb#analytics