postgresql SQL中的递归,用于更复杂的查询(与日期间隔相关)

cigdeys3  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(1)|浏览(91)

目标:创建一个递归查询,获取表的最后3个月,并在3年前每3个月对它们求和。
我有一个大表,但它有销售价格作为一列和销售日期作为一列。我想把现在()和过去3个月之间所有日期的销售价格相加,然后再把3到6个月的销售价格相加,再把6到9个月的销售价格相加,再把3年的销售价格相加。
这是我迄今为止写的代码,似乎SDATE 1迭代的方式是正确的,但我的totpaid不是

WITH RECURSIVE PARAMS AS (SELECT '07/01/2023' SDATE, '05/01/23' EDATE), --(this is going to be put in to SSRS so I am hard coding SDATE to with) 

 CteRecursive(SDATE1, totpaid, datereceived) AS (
    Select *
    From (
    select (cast((SELECT SDATE FROM PARAMS) as TIMESTAMP)) SDATE2, sum(totpaid),Datereceived
    From adjudicationresult ar
      where Locations = 'GRE'
      GROUP BY Datereceived
     ) cte
      Where Datereceived <= (cast((SDATE2 ) as date)- (INTERVAL '1 Month' ))
        and Datereceived >= (cast((SDATE2 ) as date)- (INTERVAL '3 Month' ))
--**This is where I think I went wrong trying to get SDATE2 to update

  UNION ALL
    SELECT (SDATE1 - INTERVAL '3 MONTH') SDate3 ,totpaid, Datereceived
    FROM t
    WHERE SDATE1 > (cast((SELECT SDATE FROM PARAMS) as date)- (INTERVAL '12 Month' ))
    GROUP BY t.SDATE1, t.totpaid , t.Datereceived
)
SELECT Sdate1, (totpaid), datereceived
 FROM t
ORDER BY TotPaid;

字符串
我的结果是
| 支付总额|收到的日期| daterecived |
| --|--| ------------ |
| -49.74 |2023-05-21 2023-05-21 2023-05-21| 2023-05-21 |
| -49.74 |2023-05-21 2023-05-21 2023-05-21| 2023-05-21 |
| -49.74 |2023-05-21 2023-05-21 2023-05-21| 2023-05-21 |
| -49.74 |2023-05-21 2023-05-21 2023-05-21| 2023-05-21 |
| -49.74 |2023-05-21 2023-05-21 2023-05-21| 2023-05-21 |
| 二十二点零八分|2023-05-07 2023-05-07| 2023-05-07 |
| 二十二点零八分|2023-05-07 2023-05-07| 2023-05-07 |
| 二十二点零八分|2023-05-07 2023-05-07| 2023-05-07 |
或者如果按sdate 1排序,则datecerved
| 支付总额|收到的日期| daterecived |
| --|--| ------------ |
| 1587.84| 2023-04-01 2023-04-01| 2023-04-01 |
| 285.83| 2023-04-02 2023-04-02| 2023-04-02 |
| 174746.59| 2023-04-03 2023-04-03| 2023-04-03 |
| 63322.25| 2023-04-04 2023-04-04| 2023-04-04 |
| 38465.58| 2023-04-05 2023-04-05 2023-04-05| 2023-04-05 |
| 30848.58| 2023-04-06 2023-04-06| 2023-04-06 |
| 527.58| 2023-04-07 2023-04-07| 2023-04-07 |
| 67957.30| 2023-04-08 2023-04-08| 2023-04-08 |
| 83921.99| 2023-04-10| 2023-04-10 |
| 39771.13| 2023-04-11 2023-04-11 2023-04-11| 2023-04-11 |
| 64172.55| 2023-04-12 2023-04-12 2023-04-12| 2023-04-12 |
我的问题是为什么它循环为相同的天为每个sdate 1(即2023-01-01:2023-04-01等)和为什么迭代每一天而不是求和整个季度

yqkkidmi

yqkkidmi1#

一种更传统、可能性能更好的方法是生成一系列日期范围,然后通过这些范围进行聚合。例如:

SELECT
      r AS start_date
    , r + INTERVAL '3 months' AS end_date
    , sum(totpaid)
FROM
    generate_series(
        CURRENT_DATE - INTERVAL '3 years',
        CURRENT_DATE - INTERVAL '3 months',
        INTERVAL '3 months'
    ) AS r
LEFT JOIN adjudicationresult AS s ON s.Datereceived >= r
                                 AND s.Datereceived < r + INTERVAL '3 months'
GROUP BY
      r
    , r + INTERVAL '3 months'

字符串
| 结束日期|总和| sum |
| --|--| ------------ |
| 2020-10-18 00:00:00|三百零三| 300.03 |
| 2019 -07-18 00:00:00| * 空 *| null |
| 2019 -01-18 00:00:00|三百零三| 300.03 |
| 2019 -01-18 00:00:00| * 空 *| null |
| 2019 - 06 - 28 00:00:00|三百零三| 300.03 |
| 2019 -01-18 00:00:00|四百零四| 400.04 |
| 2021-07-18 00:00:00|三百零三| 300.03 |
| 2021-04-18 00:00:00|三百零三| 300.03 |
| 2019 -04-18 00:00:00| * 空 *| null |
| 2019 - 09 - 28 00:00:00| * 空 *| null |
| 2019 -04-18 00:00:00| * 空 *| null |
| 2019 -07-18 00:00:00| * 空 *| null |
请参阅此演示here

相关问题