SQL - 计算每条记录过去 24 小时的深度进度总和?

发布于 2025-01-15 22:43:34 字数 850 浏览 0 评论 0原文

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏闹i 2025-01-22 22:43:34

使用此 CTE 获取数据:

WITH data AS (
    SELECT 
    *
    FROM VALUES
        (306, '2022-03-21 01:00:00'::timestamp, 0.5),  
        (306, '2022-03-21 08:00:00'::timestamp, 0.5),  
        (306, '2022-03-21 16:00:00'::timestamp, 0.5),  
        (306, '2022-03-21 21:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 02:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 06:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 12:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 18:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 22:00:00'::timestamp, 0.5)
        v(id, timepoint, depth_progress)
)

此 SQL 给出:

SELECT d.id, 
    d.timepoint, 
    sum(d2.depth_progress)
FROM data AS d
JOIN data AS d2 
    ON d.id = d2.id 
        AND d2.timepoint between dateadd(day,-1,d.timepoint) and d.timepoint
GROUP BY 1,2
ORDER BY 1,2;

给出:

IDTIMEPOINTSUM(D2.DEPTH_PROGRESS)
3062022-03-21 01:00:00.0000.5
3062022-03-21 08:00:00.0001
3062022-03-21 16:00:00.0001.5
3062022-03-21 21:00:00.0002
3062022-03-22 02:00:00.0002
3062022-03-22 06:00:00.0002.5
3062022-03-22 12:00:00.0002.5
3062022-03-22 18:00:00.0002.5
3062022-03-22 22:00:00.0002.5

如果您的表为“邮件”,我会预先调整该数据喜欢:

WITH massive_pre_condition as (
    select *,
        dateadd(day,-1,timepoint) as minus_one_day,
        timepoint::date as d1,
        minus_one_day::date as d2
    FROM data
)
SELECT d.id, 
    d.timepoint, 
    sum(d2.depth_progress)
FROM massive_pre_condition AS d
JOIN massive_pre_condition AS d2 
    ON d.id = d2.id 
        AND d2.d1 IN (d.d1, d.d2)
        AND d2.timepoint between d.minus_one_day and d.timepoint
GROUP BY 1,2
ORDER BY 1,2;

Using this CTE for data:

WITH data AS (
    SELECT 
    *
    FROM VALUES
        (306, '2022-03-21 01:00:00'::timestamp, 0.5),  
        (306, '2022-03-21 08:00:00'::timestamp, 0.5),  
        (306, '2022-03-21 16:00:00'::timestamp, 0.5),  
        (306, '2022-03-21 21:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 02:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 06:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 12:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 18:00:00'::timestamp, 0.5),  
        (306, '2022-03-22 22:00:00'::timestamp, 0.5)
        v(id, timepoint, depth_progress)
)

this SQL gives:

SELECT d.id, 
    d.timepoint, 
    sum(d2.depth_progress)
FROM data AS d
JOIN data AS d2 
    ON d.id = d2.id 
        AND d2.timepoint between dateadd(day,-1,d.timepoint) and d.timepoint
GROUP BY 1,2
ORDER BY 1,2;

gives:

IDTIMEPOINTSUM(D2.DEPTH_PROGRESS)
3062022-03-21 01:00:00.0000.5
3062022-03-21 08:00:00.0001
3062022-03-21 16:00:00.0001.5
3062022-03-21 21:00:00.0002
3062022-03-22 02:00:00.0002
3062022-03-22 06:00:00.0002.5
3062022-03-22 12:00:00.0002.5
3062022-03-22 18:00:00.0002.5
3062022-03-22 22:00:00.0002.5

and if your table as "missive" I would pre-condition that data like:

WITH massive_pre_condition as (
    select *,
        dateadd(day,-1,timepoint) as minus_one_day,
        timepoint::date as d1,
        minus_one_day::date as d2
    FROM data
)
SELECT d.id, 
    d.timepoint, 
    sum(d2.depth_progress)
FROM massive_pre_condition AS d
JOIN massive_pre_condition AS d2 
    ON d.id = d2.id 
        AND d2.d1 IN (d.d1, d.d2)
        AND d2.timepoint between d.minus_one_day and d.timepoint
GROUP BY 1,2
ORDER BY 1,2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文