简体   繁体   English

MySQL查询累计和(案例:当期无订单时,仍为上期的值)

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

I have order table with order date of course and its amount.我有订单表,当然还有订单日期和金额。

I want to sum the amount of previous period with current period and resulting table like this我想将上一期的金额与当前期和这样的结果表相加

my current result我目前的结果

the query I have tried was我试过的查询是

    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

My goals is when on such month there were no order, the amount are 0 while the cumulative amount must be followed the previous month.我的目标是当月没有订单时,金额为0,而累积金额必须遵循上个月。 The result of what I need are我需要的结果是

My goals result我的目标结果

You must add one more recursive CTE and generate base dates list (calendar) using min.您必须再添加一个递归 CTE 并使用 min 生成基准日期列表(日历)。 and max.和最大。 dates from your data table as generated range borders (or set these borders as parameters) then LEFT JOIN your table to it.将数据表中的日期作为生成的范围边界(或将这些边界设置为参数),然后将表连接到它。 In window function you'd use the dates from this base table which will contain all needed dates.在窗口函数中,您将使用此基表中的日期,该基表将包含所有需要的日期。

Simple DEMO:简单的演示:

 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; 
the_date日期 the_value价值 cumulative累积
2022-03-04 2022-03-04 1 1 1 1
2022-03-05 2022-03-05 2 2 3 3
2022-03-07 2022-03-07 3 3 6 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); 
the_date日期 the_value价值 cumulative累积
2022-03-04 2022-03-04 1 1 1 1
2022-03-05 2022-03-05 2 2 3 3
2022-03-06 2022-03-06 null无效的 3 3
2022-03-07 2022-03-07 3 3 6 6

db<>fiddle here db<> 在这里摆弄


The variant for different id values不同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;
id |编号 | the_date | the_date | the_value |价值 | cumulative累积
-: | -: | :--------- | :--------- | --------: | --------: | ---------: ---------:
 1 | 1 | 2022-03-04 | 2022-03-04 | 1 | 1 | 1 1
 1 | 1 | 2022-03-05 | 2022-03-05 | 2 | 2 | 3 3
 1 | 1 | 2022-03-07 | 2022-03-07 | 3 | 3 | 6 6
 2 | 2 | 2022-03-04 | 2022-03-04 | 4 | 4 | 4 4
 2 | 2 | 2022-03-06 | 2022-03-06 | 5 | 5 | 9 9
 2 | 2 | 2022-03-08 | 2022-03-08 | 6 | 6 | 15 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 | the_date | id |编号 | the_value |价值 | cumulative累积
:--------- | :--------- | -: | -: | --------: | --------: | ---------: ---------:
2022-03-04 | 2022-03-04 | 1 | 1 | 1 | 1 | 1 1
2022-03-05 | 2022-03-05 | 1 | 1 | 2 | 2 | 3 3
2022-03-06 | 2022-03-06 | 1 | 1 | null || 3 3
2022-03-07 | 2022-03-07 | 1 | 1 | 3 | 3 | 6 6
2022-03-08 | 2022-03-08 | 1 | 1 | null || 6 6
2022-03-04 | 2022-03-04 | 2 | 2 | 4 | 4 | 4 4
2022-03-05 | 2022-03-05 | 2 | 2 | null || 4 4
2022-03-06 | 2022-03-06 | 2 | 2 | 5 | 5 | 9 9
2022-03-07 | 2022-03-07 | 2 | 2 | null || 9 9
2022-03-08 | 2022-03-08 | 2 | 2 | 6 | 6 | 15 15

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM