SQL - 计算每条记录过去 24 小时的深度进度总和?
SQL - 计算每条记录过去 24 小时的 md_delta 总和。
这是我拍摄的示例
基本上,我试图创建一个返回未更改的基表的查询,这样对于左表中的每一行,它返回 MD_DELTA 的总和以前的每个唯一 ID 24 小时。
我想我可能需要使用相关子查询,但它似乎无法正常工作。
这是一个代码示例:
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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此 CTE 获取数据:
此 SQL 给出:
给出:
如果您的表为“邮件”,我会预先调整该数据喜欢:
Using this CTE for data:
this SQL gives:
gives:
and if your table as "missive" I would pre-condition that data like: