簡體   English   中英

MySQL查詢累計和(案例:當期無訂單時,仍為上期的值)

[英]MySQL Query of Cumulative Sum (Case: when there is no order on such period, the value still on previous period)

我有訂單表,當然還有訂單日期和金額。

我想將上一期的金額與當前期和這樣的結果表相加

我目前的結果

我試過的查詢是

    with data as(
select  
        lead_id,
        no_pks,
        customer_name,
        point_name,
        funder_id,
        DATE_FORMAT(order_date,'%Y-%m') as year_and_month_order,
        sum(total_amount) as outstanding
    from 
        mall_order_list_payment
    group by
        lead_id,
        no_pks,
        customer_name,
        point_name,
        funder_id,
        year_and_month_order
)

select 
    *,
    sum(outstanding) over(partition by lead_id,no_pks order by year_and_month_order) as cumulative_outstanding
from
    data
order by lead_id,no_pks

我的目標是當月沒有訂單時,金額為0,而累積金額必須遵循上個月。 我需要的結果是

我的目標結果

您必須再添加一個遞歸 CTE 並使用 min 生成基准日期列表(日歷)。 和最大。 將數據表中的日期作為生成的范圍邊界(或將這些邊界設置為參數),然后將表連接到它。 在窗口函數中,您將使用此基表中的日期,該基表將包含所有需要的日期。

簡單的演示:

 CREATE TABLE test (the_date DATE, the_value INT); INSERT INTO test SELECT '2022-03-04', 1 UNION ALL SELECT '2022-03-05', 2 UNION ALL SELECT '2022-03-07', 3 ;
 SELECT *, SUM(the_value) OVER (ORDER BY the_date) cumulative FROM test; 
日期 價值 累積
2022-03-04 1 1
2022-03-05 2 3
2022-03-07 3 6
 WITH RECURSIVE cte AS ( SELECT MIN(the_date) the_date FROM test UNION ALL SELECT the_date + INTERVAL 1 DAY FROM cte WHERE the_date < ( SELECT MAX(the_date) FROM test ) ) SELECT *, SUM(test.the_value) OVER (ORDER BY the_date) cumulative FROM cte LEFT JOIN test USING (the_date); 
日期 價值 累積
2022-03-04 1 1
2022-03-05 2 3
2022-03-06 無效的 3
2022-03-07 3 6

db<> 在這里擺弄


不同id值的變體

CREATE TABLE test (id INT, the_date DATE, the_value INT); INSERT INTO test SELECT 1, '2022-03-04', 1 UNION ALL SELECT 1, '2022-03-05', 2 UNION ALL SELECT 1, '2022-03-07', 3 UNION ALL SELECT 2, '2022-03-04', 4 UNION ALL SELECT 2, '2022-03-06', 5 UNION ALL SELECT 2, '2022-03-08', 6 ;
 SELECT *, SUM(the_value) OVER (PARTITION BY id ORDER BY the_date) cumulative FROM test;
編號 |  the_date | 價值 | 累積
 -: |  :--------- |  --------: |  ---------:
  1 |  2022-03-04 |  1 |  1
  1 |  2022-03-05 |  2 |  3
  1 |  2022-03-07 |  3 |  6
  2 |  2022-03-04 |  4 |  4
  2 |  2022-03-06 |  5 |  9
  2 |  2022-03-08 |  6 |  15
WITH RECURSIVE cte1 AS ( SELECT MIN(the_date) the_date FROM test UNION ALL SELECT the_date + INTERVAL 1 DAY FROM cte1 WHERE the_date < ( SELECT MAX(the_date) FROM test ) ), cte2 AS ( SELECT DISTINCT id FROM test ) SELECT *, SUM(test.the_value) OVER (PARTITION BY id ORDER BY the_date) cumulative FROM cte1 CROSS JOIN cte2 LEFT JOIN test USING (id, the_date);
 the_date | 編號 | 價值 | 累積
 :--------- |  -: |  --------: |  ---------:
 2022-03-04 |  1 |  1 |  1
 2022-03-05 |  1 |  2 |  3
 2022-03-06 |  1 | |  3
 2022-03-07 |  1 |  3 |  6
 2022-03-08 |  1 | |  6
 2022-03-04 |  2 |  4 |  4
 2022-03-05 |  2 | |  4
 2022-03-06 |  2 |  5 |  9
 2022-03-07 |  2 | |  9
 2022-03-08 |  2 |  6 |  15

db<> 在這里擺弄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM