Problem description & analysis:
A certain database table stores time-series data, with intervals of several seconds between each record.
[Source Table]
Task: Now we need to do a group and aggregation every minute, summarizing data for 5 minutes each time. For example, generate three records in minutes 1, 2, and 3, and summarize the data for minutes 1–5, 2–6, and 3–7 respectively.
Code comparisons:
SQL:
SELECT
[From], DATEADD(MINUTE, 1, [To]) [To], payload
FROM (
SELECT
dt, MIN(dt) OVER(ORDER BY dt ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) [From],
dt [To], SUM(payload) OVER(ORDER BY dt ROWS BETWEEN 4 PRECEDING AND CURRENT ROW) payload
FROM (
SELECT
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, dt), 0) dt,
SUM(payload) payload
FROM #tmstmp
GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, dt), 0)
) q
) q
WHERE DATEDIFF(MINUTE, [From], [To]) > 3
SQL needs to implement it using nested subqueries and multiple window functions, which makes the code cumbersome.
SPL: SPL provides the syntax for directly accessing positions.
👉🏻 Try.DEMO
A1: Load data.
A2: Group and aggregate by minute.
A3: Generate a new two-dimensional table, where To is taken from the current record, From is taken from 5 minutes after the current record, and the payload summarizes the interval from the current record to the 4th record.
A4: Take from the first item backward to the fifth item.
Free to Try, Powerful to Use — esProc SPL FREE Download.
Top comments (3)
these window functions always get my brain twisted - i swear there's gotta be a simpler way for this kinda rolling stuff you think most people overcomplicate their queries or it's just how sql's built
Totally feel you on that! Window functions can get pretty tangled, especially for rolling aggregations. I do think SQL’s design makes these patterns a bit verbose by nature. That’s actually why I enjoy exploring SPL—it often lets you express these same calculations in a much simpler way. Might be worth a look if you’re into cleaner, more readable approaches!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.