繁体   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