Window Functions

When do you use window functions over aggregate functions?

  1. When you want to measure trends or changes over rows.

  2. When you want to rank a column to use for outreach/prioritization.

Example:

What is window function?

Window Function: A window function is a calculation across a set of rows in a table that are somehow related to the current row. This means we’re typically:

  1. Calculating running totals that incorporate the current row or,

  2. Ranking records across rows, inclusive of the current one

A window function is similar to aggregate functions combined with GROUP BY clauses but have one key difference: Window functions retain the total number of rows between the input table and the output table (or result). Behind the scenes, the window function is able to access more than just the current row of the query result.

a window function allows users to compare one row to another without doing any joins.

When window functions are used, you’ll notice new column names like the following:

  • Average running price

  • Running total orders

  • Running sum sales

  • Rank

  • Percentile

Core

  • PARTITION BY: A sub-clause of the OVER clause. Similar to GROUP BY.

  • OVER: Typically precedes the partition by that signals what to “GROUP BY”.

  • Aggregates: Aggregate functions that are used in window functions, too (e.g., sum, count, avg).

AGGREGATE_FUNCTION (column_1) OVER
 (PARTITION BY column_2 ORDER BY column_3)
  AS new_column_name;

Ranking

  • ROW_NUMBER(): Ranking is distinct amongst records even with ties in what the table is ranked against.

  • RANK(): Ranking function where a row could get the same rank if they have the same value and ranks skip for subsequent values.

  • DENSE_RANK(): Ranking function similar to RANK() but ranks are not skipped with ties.

SELECT ROW_NUMBER() OVER(ORDER BY date_time) AS rwrank,
       RANK() OVER(ORDER BY date_time) AS rrank,
       DENSE_RANK() OVER(ORDER BY date_time) AS drrank,
       date_time
FROM   db;

Advanced

  • Aliases: Shorthand that can be used if there are several window functions in one query.

SELECT order_id,
       order_total,
       order_price,
       SUM(order_total) OVER monthly_window AS running_monthly_sales,
       COUNT(order_id) OVER monthly_window AS running_monthly orders,
       AVG(order_price) OVER monthly_window AS average_monthly_price
FROM   amazon_sales_db
WHERE  order_date < '2017-01-01'
WINDOW monthly_window AS
       (PARTITION BY month(order_date) ORDER BY order_date);
  • Percentiles: Defines what percentile a value falls into over the entire table.

NTILE(# of buckets) OVER (ORDER BY ranking_column) AS new_column_name
SELECT  customer_id,
        composite_score,
        NTILE(100) OVER(ORDER BY composite_score) AS percentile
FROM    customer_lead_score;
  • Lag/Lead: Calculating differences between rows’ values.

SELECT account_id,
       standard_sum,
       LAG(standard_sum) OVER(ORDER BY standard_sum) AS lag,
       LEAD(standard_sum) OVER (ORDER BY standard_sum) AS lead,
       standard_sum - LAG(standard_sum) OVER (ORDER BY standard_sum) AS lag_diff,
       LEAD(standard_sum) OVER (ORDER BY standard_sum) - standard_sum AS lead_diff
FROM (
       SELECT account_id,
              SUM(standard_qty) AS standard_sum
       FROM orders
       GROUP BY 1
) sub

Last updated