Sql 创建滚动周期
我希望每6个月创建一个滚动周期,我不确定最好的方法。我猜这可能是递归的 我有一个包含以下类似数据的付款表:Sql 创建滚动周期,sql,sql-server,datetime,sum,aggregate-functions,Sql,Sql Server,Datetime,Sum,Aggregate Functions,我希望每6个月创建一个滚动周期,我不确定最好的方法。我猜这可能是递归的 我有一个包含以下类似数据的付款表: CREATE TABLE payments ([id] int, [payment_month] int, [payment_date] datetime, [payment_amount] int) ; INSERT INTO payments ([id], [payment_month], [payment_date], [payment_amount]) V
CREATE TABLE payments
([id] int, [payment_month] int, [payment_date] datetime, [payment_amount] int)
;
INSERT INTO payments
([id], [payment_month], [payment_date], [payment_amount])
VALUES
(1, 201911, '2019-11-01 00:00:00', 50),
(1, 201912, '2019-12-01 00:00:00', 50),
(1, 202001, '2020-01-01 00:00:00', 50),
(1, 202002, '2020-02-01 00:00:00', 50),
(1, 202003, '2020-03-01 00:00:00', 50),
(1, 202004, '2020-04-01 00:00:00', 50),
(1, 202005, '2020-05-01 00:00:00', 50),
(1, 202006, '2020-06-01 00:00:00', 25),
(1, 202007, '2020-07-01 00:00:00', 50),
(1, 202008, '2020-08-01 00:00:00', 50),
(1, 202009, '2020-09-01 00:00:00', 15),
(2, 201911, '2019-11-01 00:00:00', 50),
(2, 201912, '2019-12-01 00:00:00', 50),
(2, 202001, '2020-01-01 00:00:00', 25),
(2, 202002, '2020-02-01 00:00:00', 50),
(2, 202003, '2020-03-01 00:00:00', 45),
(2, 202004, '2020-04-01 00:00:00', 45),
(2, 202004, '2020-04-10 00:00:00', 20),
(2, 202005, '2020-05-01 00:00:00', 25),
(3, 202004, '2020-04-01 00:00:00', 50),
(3, 202005, '2020-05-01 00:00:00', 50),
(3, 202006, '2020-06-01 00:00:00', 50),
(3, 202007, '2020-07-01 00:00:00', 50),
(3, 202008, '2020-08-01 00:00:00', 50),
(3, 202009, '2020-09-01 00:00:00', 300)
;
我还使用了一个日历表,它返回如下数据:
CREATE TABLE calendar
([CalendarPeriod] int, [CalendarDate] datetime)
;
INSERT INTO calendar
([CalendarPeriod], [CalendarDate])
VALUES
(202004, '2020-04-30 00:00:00'),
(202005, '2020-05-31 00:00:00'),
(202006, '2020-06-30 00:00:00'),
(202007, '2020-07-31 00:00:00'),
(202008, '2020-08-31 00:00:00'),
(202009, '2020-09-30 00:00:00')
;
我希望得到一个输出,其中日历
中的每个日历期
与自身和之前的5个月配对<代码>日历期作为报告期,每个月的滚动期为6个月。我的最终目标是通过payment\u month
将payment\u amounts
加入每个报告期,其中payment\u month
等于报告期中的一个滚动月
编辑:
我对几个月和滚动期的预期产出如下表所示。如果需要,我可以通过CalendarPeriod
操作表以进行聚合
+----------------+----------------+----------------+
| CalendarPeriod | rolling_period | rolling_amount |
+----------------+----------------+----------------+
| 202004 | 201911 | 100 |
+----------------+----------------+----------------+
| 202004 | 201912 | 100 |
+----------------+----------------+----------------+
| 202004 | 202001 | 75 |
+----------------+----------------+----------------+
| 202004 | 202002 | 100 |
+----------------+----------------+----------------+
| 202004 | 202003 | 95 |
+----------------+----------------+----------------+
| 202004 | 202004 | 165 |
+----------------+----------------+----------------+
| 202005 | 201912 | 100 |
+----------------+----------------+----------------+
| 202005 | 202001 | 75 |
+----------------+----------------+----------------+
| 202005 | 202002 | 100 |
+----------------+----------------+----------------+
| 202005 | 202003 | 95 |
+----------------+----------------+----------------+
| 202005 | 202004 | 165 |
+----------------+----------------+----------------+
| 202005 | 202005 | 125 |
+----------------+----------------+----------------+
| 202006 | 202001 | 75 |
+----------------+----------------+----------------+
| 202006 | 202002 | 100 |
+----------------+----------------+----------------+
| 202006 | 202003 | 95 |
+----------------+----------------+----------------+
| 202006 | 202004 | 165 |
+----------------+----------------+----------------+
| 202006 | 202005 | 125 |
+----------------+----------------+----------------+
| 202006 | 202006 | 75 |
+----------------+----------------+----------------+
| 202007 | 202002 | 100 |
+----------------+----------------+----------------+
| 202007 | 202003 | 95 |
+----------------+----------------+----------------+
| 202007 | 202004 | 165 |
+----------------+----------------+----------------+
| 202007 | 202005 | 125 |
+----------------+----------------+----------------+
| 202007 | 202006 | 75 |
+----------------+----------------+----------------+
| 202007 | 202007 | 100 |
+----------------+----------------+----------------+
| 202008 | 202003 | 95 |
+----------------+----------------+----------------+
| 202008 | 202004 | 165 |
+----------------+----------------+----------------+
| 202008 | 202005 | 125 |
+----------------+----------------+----------------+
| 202008 | 202006 | 75 |
+----------------+----------------+----------------+
| 202008 | 202007 | 100 |
+----------------+----------------+----------------+
| 202008 | 202008 | 100 |
+----------------+----------------+----------------+
| 202009 | 202004 | 165 |
+----------------+----------------+----------------+
| 202009 | 202005 | 125 |
+----------------+----------------+----------------+
| 202009 | 202006 | 75 |
+----------------+----------------+----------------+
| 202009 | 202007 | 100 |
+----------------+----------------+----------------+
| 202009 | 202008 | 100 |
+----------------+----------------+----------------+
| 202009 | 202009 | 315 |
+----------------+----------------+----------------+
提前感谢您的帮助。据我了解您的问题,您希望在过去6个月内对每个期间进行滚动付款 我不认为您真的需要显示中间数据集来实现此目标。您可以只使用窗口功能:
select c.calendarperiod,
sum(p.payment_amount) month_amount,
sum(sum(p.payment_amount)) over(
order by c.calendarperiod
rows between 5 preceding and current row
) rolling_6_month_amount
from calendar c
left join payments p
on p.payment_date >= datefromparts(year(c.calendardate), month(c.calendardate), 1)
and p.payment_date < dateadd(day, 1, c.calendardate)
group by c.calendarperiod
选择c.calendarperiod,
金额(p.付款金额)月金额,
金额(金额(p.付款金额)超过(
按日历周期订购
前5行和当前行之间的行
)滚动6个月金额
从日历c
左联付款
付款日期>=datefromparts(年(c.calendardate),月(c.calendardate),1)
和p.付款日期<日期添加(第1天,c.日历日期)
按日历周期分组
该查询从日历开始,并为每个月带来相应的付款-我调整了日期间隔,以防在该月的最后一天内有付款。然后,我们按周期进行聚合。最后,我们可以使用窗口功能回顾过去5个期间,并对相应的付款进行汇总。我理解您的问题,您希望对每个期间的过去6个月的付款进行滚动汇总 我不认为您真的需要显示中间数据集来实现此目标。您可以只使用窗口功能:
select c.calendarperiod,
sum(p.payment_amount) month_amount,
sum(sum(p.payment_amount)) over(
order by c.calendarperiod
rows between 5 preceding and current row
) rolling_6_month_amount
from calendar c
left join payments p
on p.payment_date >= datefromparts(year(c.calendardate), month(c.calendardate), 1)
and p.payment_date < dateadd(day, 1, c.calendardate)
group by c.calendarperiod
选择c.calendarperiod,
金额(p.付款金额)月金额,
金额(金额(p.付款金额)超过(
按日历周期订购
前5行和当前行之间的行
)滚动6个月金额
从日历c
左联付款
付款日期>=datefromparts(年(c.calendardate),月(c.calendardate),1)
和p.付款日期<日期添加(第1天,c.日历日期)
按日历周期分组
该查询从日历开始,并为每个月带来相应的付款-我调整了日期间隔,以防在该月的最后一天内有付款。然后,我们按周期进行聚合。最后,我们可以使用窗口函数回顾过去5个期间,并计算相应的付款金额。您是否尝试过在
日历
和日历
之间使用连接
来匹配6个月的窗口?我还没有尝试过,尽管我相信会有办法做到这一点。如果我可以避免很多加入更好,这是一个更大的存储过程的一部分。您是否尝试过在日历
和日历
之间使用加入
来匹配6个月的窗口?我还没有尝试过,尽管我相信会有办法做到这一点。如果我能避免很多加入更好,这是一个更大的存储过程的一部分,这很接近,但我不认为总数完全符合预期。e、 例如,202004应该是635。我想考虑在任何滚动期间支付它的代码>日历时期< /代码>。202004应包含201911和202004之间的付款数据--@GMBupdated@tlk27:请显示示例数据的结果。@tlk27:谢谢您的编辑。问题在于日历表,从2020年4月开始,而数据从2019年11月开始。如果扩展日历表,则会得到所需的结果。看这把db小提琴:谢谢你的时间和帮助,非常感谢。我不知道x前面和当前功能之间的行这很接近,但我认为总数与预期不匹配。e、 例如,202004应该是635。我想考虑在任何滚动期间支付它的代码>日历时期< /代码>。202004应包含201911和202004之间的付款数据--@GMBupdated@tlk27:请显示示例数据的结果。@tlk27:谢谢您的编辑。问题在于日历表,从2020年4月开始,而数据从2019年11月开始。如果扩展日历表,则会得到所需的结果。看这把db小提琴:谢谢你的时间和帮助,非常感谢。我不知道x之前和当前功能之间的行