NULLs in window functions

Concepts covered: Row Offsets

A window function computes a value for each row from a set of related rows. Every row stays in the output, where GROUP BY would fold them into one. Window functions get lessons of their own later in the track, and this section needs only 3 pieces of them. An OVER (...) clause after a function makes it a window function. ORDER BY inside OVER lines the rows up, and an aggregate such as SUM then covers every row from the first one through the current one. PARTITION BY inside OVER splits the rows into separate groups the way GROUP BY does, without collapsing them.

The table store_sales holds 5 days of revenue for a single store. The register feed failed on days 2 and 4, so those rows hold NULL: the store traded on those days, and nobody knows for how much. Play the query and watch each running value build up row by row.

SELECT
  day_num,
  revenue,
  SUM(revenue) OVER (ORDER BY day_num) AS running_total,
  COUNT(revenue) OVER (ORDER BY day_num) AS days_counted
FROM store_sales
store_sales
day_numrevenue
1100
2NULL
380
4NULL
5120

The running total never turns NULL. On day 2 the frame holds 100 and a NULL, and SUM skips the NULL the way it does in a grouped query, so the total stays at 100, then sits flat again on day 4. A chart of running_total shows those flat stretches as 2 days with no sales, and nothing in the output marks them as unknown. days_counted is the column that tells the truth: COUNT(revenue) counts only known values, so any day where it fails to rise is a day with a gap.

Had day 1 been NULL, its running total would be NULL too. A frame holding nothing but NULLs gives SUM no known input, and SUM with no known input returns NULL until the first real value arrives.

sensor_readings holds reading_hour 1 with reading NULL, reading_hour 2 with reading 5 and reading_hour 3 with reading 7. What does running_total hold for hours 1 and 2?

SELECT
  reading_hour,
  reading,
  SUM(reading) OVER (ORDER BY reading_hour) AS running_total
FROM sensor_readings

LAG cannot tell a gap from the edge

LAG(revenue) returns the revenue from the row before the current one, in the order OVER sets. Its optional third argument is a default for a row that has nothing before it. Before running the query, predict prev_or_zero for day 1 and for day 3.

SELECT
  day_num,
  revenue,
  LAG(revenue) OVER (ORDER BY day_num) AS prev_revenue,
  LAG(revenue, 1, 0) OVER (ORDER BY day_num) AS prev_or_zero,
  revenue - LAG(revenue) OVER (ORDER BY day_num) AS change
FROM store_sales
ORDER BY day_num
store_sales
day_numrevenue
1100
2NULL
380
4NULL
5120
Result
day_numrevenueprev_revenueprev_or_zerochange
1100NULL0NULL
2NULL100100NULL
380NULLNULLNULL
4NULL8080NULL
5120NULLNULLNULL

Day 1 gets 0: no row comes before it, so the default applies. Day 3 gets NULL: a row does come before it, and that row's revenue is NULL. The default fills a missing row and leaves a missing value alone. In prev_revenue both cases print the same NULL, so anyone reading the output can't tell which one they're looking at. The change column fares worse. All 5 of its values are NULL, because every subtraction has a NULL on one side, the edge row included. With 2 gaps in 5 days, not a single day-over-day change survives.

Carrying the last known value forward

A common repair fills each gap with the most recent known value, so day 2 shows 100 and day 4 shows 80. It takes 2 window passes. The first labels rows with a running COUNT(revenue), which rises only on rows holding a value, so each NULL row shares a label with the known row before it. The second takes MAX(revenue) over each label with PARTITION BY. Each partition holds a single known value, and MAX skips the NULLs beside it, so it hands that value to every row in the partition.

WITH grouped AS (
  SELECT
    day_num,
    revenue,
    COUNT(revenue) OVER (ORDER BY day_num) AS fill_group
  FROM store_sales
)
SELECT
  day_num,
  revenue,
  fill_group,
  MAX(revenue) OVER (PARTITION BY fill_group) AS filled_revenue
FROM grouped
ORDER BY day_num
store_sales
day_numrevenue
1100
2NULL
380
4NULL
5120
Result
day_numrevenuefill_groupfilled_revenue
11001100
2NULL1100
380280
4NULL280
51203120

fill_group reads 1, 1, 2, 2, 3, and each pair shares its known value. Swap COUNT(revenue) for COUNT(*) and every row gets a group of its own, so the NULL rows stay NULL. A series that opens with NULLs puts those rows in group 0, which holds no known value, so they stay NULL as well: forward fill has nothing to carry until the first value arrives. Whether carrying a value forward is honest depends on the data. A thermostat reading holds until the next reading, while yesterday's revenue says nothing about today's.

About this lesson

This section is part of the NULL Values: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.