Getting sum for each day based on most recent timestamp entry for each entity
1
vote
1
answer
373
views
I have a table that contains an ID, timestamp, and integer. Each row is a "snapshot" of the entity at a given time:
**TableA**
| ID | timestamp | num |
| -- | ---------- | --- |
| A | 2024-02-04 | 5 |
| B | 2024-02-04 | 3 |
| C | 2024-02-07 | 8 |
| A | 2024-02-07 | 3 |
My goal is to get a table of all days from the last year and show the sum of
num
for that given day. The sum for each day though should look at the most recent timestamp for each entity. Then if there's no data for a given day, just forward-fill until reaching the next timestamp. So the result would be:
**Goal**
| day | total |
| ---------- | ----- |
| 2024-02-04 | 8 |
| 2024-02-05 | 8 |
| 2024-02-06 | 8 |
| 2024-02-07 | 14 |
2024-02-05 and 2024-02-06 are filled from the most recent row because there is no timestamp data for them, and 2024-02-07 is 14 because entity A now has "3" instead of "5", and entity "C" has been added (3 + 8 + 3).
My approach has been to start with a recursive CTE for the days of the last year, and then left join on another CTE that contains the aggregated data.
WITH DateSequence AS (
SELECT CAST(GETDATE() AS DATE) AS [Date]
UNION ALL
SELECT DATEADD(DAY, -1, [Date])
FROM DateSequence
WHERE DATEADD(DAY, -1, [Date]) > DATEADD(DAY, -365, GETDATE())
),
Aggregator AS (
/* for each day in TableA, sum the most recent num
columns for each entity */
)
SELECT DateSequence.[Date], Aggregator.total
FROM DateSequence
LEFT JOIN Aggregator ON Aggregator.date = DateSequence.[Date]
ORDER BY DateSequence.[Date]
OPTION (MAXRECURSION 365);
The two parts I haven't worked out are the summing and the forward-filling. I'm pretty sure I need to use window functions, and need a way to say: for each day in **TableA**, sum the most recent num
columns for each entity.
Asked by Timothy Fisher
(177 rep)
May 3, 2024, 12:24 AM
Last activity: May 3, 2024, 05:54 AM
Last activity: May 3, 2024, 05:54 AM