如何根据MYSQL中的开始日期和结束日期获得运行总计

问题描述 投票:1回答:1

我有如下示例数据。

Start_Dt    End_Dt     Dur_of_months  amount 
2020-01-01  2020-04-01   4             800

我有开始日期和结束日期以及几个月的持续时间。

将数量(800)除以4 = 200。

我希望每个月的总跑步次数为负(200)。

输出:

mon_dt     amount 
Jan 2020    800
Feb 2020    600
Mar 2020    400
Apr 2020    200

我有一些代码可以增加开始日期和结束日期之间的月份

SELECT ID, DATE_FORMAT(Startdate + INTERVAL n.n MONTH, '%M %Y') AS Dates
FROM dates
JOIN (
  SELECT n10.n * 10 + n1.n * 1 AS n
  FROM (
    SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
    UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
    UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
  ) n10
  CROSS JOIN (
    SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
    UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
    UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
  ) n1
) n ON Startdate + INTERVAL n.n MONTH <= EndDate
ORDER BY ID, Startdate + INTERVAL n.n MONTH

如何在此添加运行总计。谁能建议我。

mysql sql date recursive-query
1个回答
3
投票
with recursive cte as ( select start_dt, end_dt, dur_of_months, 1 lvl, amount initial_amount, amount from mytable union all select start_dt + interval 1 month, end_dt, dur_of_months, lvl + 1, initial_amount, initial_amount * (1 - lvl / dur_of_months) from cte where start_dt < end_dt ) select date_format(start_dt, '%M %Y') mon_dt, amount from cte order by start_dt

Demo on DB Fiddle

| mon_dt | amount | | ------------- | ------ | | January 2020 | 800 | | February 2020 | 600 | | March 2020 | 400 | | April 2020 | 200 |

在早期版本中,您将从现有查询开始:
select date_format(start_dt + interval n.n month, '%M %Y') as mon_dt, amount * (1 - n / dur_of_months) amount from mytable join ( select n10.n * 10 + n1.n * 1 as n from ( select 0 n union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 ) n10 cross join ( select 0 n union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 ) n1 ) n on start_dt + interval n.n month <= end_dt order by start_dt + interval n.n month

Demo
© www.soinside.com 2019 - 2024. All rights reserved.