OSSQL.010: SQL Window Functions — Partitions, Running Totals and LAG

Two independent rack partitions with sample, power and cumulative sum values from the example

Open-Source SQL Programming • Foundational • Estimated study time: 45 minutes.

Telemetry analysts often need to retain each individual record while calculating running totals or comparing it with a preceding observation. Window functions solve that problem without collapsing rows into a single result per group. This lesson extends common table expressions and grouped aggregates.

Learning objectives

Distinguish grouped aggregates from window functions; use PARTITION BY, ORDER BY, ROW_NUMBER, SUM and LAG; specify a deterministic row order and explicit frame; distinguish a sum of power samples from energy.

Video: an extended SQL window functions tutorial, including running totals and row numbering.

Partition, order and frame

The SQLite window-function documentation explains that an aggregate window function returns a value for each row over its frame. PARTITION BY divides rows into independent groups; ORDER BY inside OVER specifies the sequence within each group. The outer ORDER BY determines presentation order. The explicit ROWS frame used here accumulates records through the current row. A default RANGE frame can include tied peers, so do not assume it is identical to ROWS.

Front of server racks at NERSC
Derrick Coetzee / Wikimedia Commons, CC0. Historical contextual photograph; the example uses synthetic readings.

Worked example: rack power readings

Use the ordering principles from ORDER BY and WHERE filtering. The composite primary key makes the sample sequence unique per rack. Run in an empty local SQLite database.

CREATE TABLE readings (
  rack TEXT, sample INTEGER, watts INTEGER,
  PRIMARY KEY(rack,sample)
);
INSERT INTO readings VALUES
 ('A',1,100),('A',2,150),('A',3,120),
 ('B',1,80),('B',2,90);
SELECT rack,sample,watts,
 ROW_NUMBER() OVER (
   PARTITION BY rack ORDER BY sample
 ) AS seq,
 SUM(watts) OVER (
   PARTITION BY rack ORDER BY sample
   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
 ) AS running_sum,
 LAG(watts) OVER (
   PARTITION BY rack ORDER BY sample
 ) AS previous
FROM readings
ORDER BY rack,sample;

SQLite 3.46.1: all five row assertions passed in an in-memory database. Output columns are rack, sample, watts, seq, running_sum and previous.

A | 1 | 100 | 1 | 100 | NULL
A | 2 | 150 | 2 | 250 | 100
A | 3 | 120 | 3 | 370 | 150
B | 1 | 80  | 1 | 80  | NULL
B | 2 | 90  | 2 | 170 | 80

Power samples are not energy

If each measurement represents the average power for an equal interval d in seconds, energy E = d × ΣP / 3600 Wh, where P is power in watts. At 60 seconds per interval, rack A consumes approximately 6.17 Wh. The raw sum 370 is a numerical sum of watt samples, not an energy reading. Irregular intervals require interval-specific integration. Do not feed synthetic or unvalidated data into equipment controls.

Community example discusses running totals and ranking in analytics.

Exercises and answers

A. How many result rows remain? B. Why does B start with sequence 1? C. What is A’s preceding value at sample 3? D. Why does the first LAG return NULL? E. What is the energy for B at 60-second intervals? Answers: A. Five. B. Separate partition. C. 150. D. No preceding row. E. Approximately 2.83 Wh. Cover: original technical illustration of the example.

Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

BitcoinVersus.Tech donation QR code for 5,000 satoshis

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.

BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply