SQL - Calculate sum of md_delta for previous 24 hrs for every record.
Here is an example of what I am shooting for
Basically, I am trying to create a query that returns the base table unaltered, such that for each row in left table it returns the sum of MD_DELTA over the previous 24 hrs for each unique ID.
I'm thinking I would likely need to use a correlated subquery but its does not seem to be working correctly.
Here is an code example:
SELECT
PG."INFO_ID" AS ID
,PG."RANK"
,PG."TIMEPOINT"
,PG."MD_DELTA"
,G."TOTAL_24HR_DELTA"
FROM CTE_PGROUPS AS PG
LEFT JOIN(
/*
CORRELATED SUB-QUERY TO GET THE SUM OF THE DEPTH_PROGRESS FROM THEPREVIOUS
24-HOUR TIME PERIOD FOR EACH
*/
SELECT
G."TIMEPOINT"
,SUM(G."MD_DELTA") AS TOTAL_24HR_DELTA
FROM CTE_PGROUPS AS G
WHERE G."TIMEPOINT" >= DATEADD('DAY',-1, G."TIMEPOINT")
GROUP BY G."TIMEPOINT"
) AS G ON PG."TIMEPOINT" = G."TIMEPOINT"
