SQL 服务器,运行 总数,每个月重置并再次求和
SQL Server, running total, reset for each month and sum again
我有一个日历 table,其中标有工作日。
现在我需要一个名为“current_working_day”的 运行 总计,它总结了一个月结束前的工作日并再次重新启动。
这是我的查询:
select
WDAYS.Date,
WDAYS.DayName,
WDAYS.WorkingDay,
sum(WDAYS.WorkingDay) OVER(order by (Date), MONTH(Date), YEAR(Date)) as 'current_working_day',
sum(WDAYS.WorkingDay) OVER(PARTITION by YEAR(WDAYS.Date), MONTH(WDAYS.Date) ) total_working_days_per_month
from WDAYS
where YEAR(WDAYS.Date) = 2022
这是我当前的输出
Date
DayName
WorkingDay
current_working_day
total_working_days_per_month
2022-01-27
Thursday
1
19
21
2022-01-28
Friday
1
20
21
2022-01-29
Saturday
0
20
21
2022-01-30
Sunday
0
20
21
2022-01-31
Monday
1
21
21
2022-02-01
Tuesday
1
22
20
2022-02-02
Wednesday
1
23
20
2022-02-03
Thursday
1
24
20
但是“current_workind_day”栏应该是这样的
Date
DayName
WorkingDay
current_working_day
total_working_days_per_month
2022-01-27
Thursday
1
19
21
2022-01-28
Friday
1
20
21
2022-01-29
Saturday
0
20
21
2022-01-30
Sunday
0
20
21
2022-01-31
Monday
1
21
21
2022-02-01
Tuesday
1
1
20
2022-02-02
Wednesday
1
2
20
2022-02-03
Thursday
1
3
20
感谢任何建议。
您可以尝试将 PARTITION by
与 EOMONTH
函数一起使用,这可能会得到相同的结果但性能更好,那么您可能只需要按 Date
排序而不是使用该函数带日期。
select
WDAYS.Date,
WDAYS.DayName,
WDAYS.WorkingDay,
sum(WDAYS.WorkingDay) OVER(PARTITION by EOMONTH(WDAYS.Date) order by Date) as 'current_working_day',
sum(WDAYS.WorkingDay) OVER(PARTITION by EOMONTH(WDAYS.Date) ) total_working_days_per_month
from WDAYS
where YEAR(WDAYS.Date) = 2022
我有一个日历 table,其中标有工作日。 现在我需要一个名为“current_working_day”的 运行 总计,它总结了一个月结束前的工作日并再次重新启动。
这是我的查询:
select
WDAYS.Date,
WDAYS.DayName,
WDAYS.WorkingDay,
sum(WDAYS.WorkingDay) OVER(order by (Date), MONTH(Date), YEAR(Date)) as 'current_working_day',
sum(WDAYS.WorkingDay) OVER(PARTITION by YEAR(WDAYS.Date), MONTH(WDAYS.Date) ) total_working_days_per_month
from WDAYS
where YEAR(WDAYS.Date) = 2022
这是我当前的输出
Date | DayName | WorkingDay | current_working_day | total_working_days_per_month |
---|---|---|---|---|
2022-01-27 | Thursday | 1 | 19 | 21 |
2022-01-28 | Friday | 1 | 20 | 21 |
2022-01-29 | Saturday | 0 | 20 | 21 |
2022-01-30 | Sunday | 0 | 20 | 21 |
2022-01-31 | Monday | 1 | 21 | 21 |
2022-02-01 | Tuesday | 1 | 22 | 20 |
2022-02-02 | Wednesday | 1 | 23 | 20 |
2022-02-03 | Thursday | 1 | 24 | 20 |
但是“current_workind_day”栏应该是这样的
Date | DayName | WorkingDay | current_working_day | total_working_days_per_month |
---|---|---|---|---|
2022-01-27 | Thursday | 1 | 19 | 21 |
2022-01-28 | Friday | 1 | 20 | 21 |
2022-01-29 | Saturday | 0 | 20 | 21 |
2022-01-30 | Sunday | 0 | 20 | 21 |
2022-01-31 | Monday | 1 | 21 | 21 |
2022-02-01 | Tuesday | 1 | 1 | 20 |
2022-02-02 | Wednesday | 1 | 2 | 20 |
2022-02-03 | Thursday | 1 | 3 | 20 |
感谢任何建议。
您可以尝试将 PARTITION by
与 EOMONTH
函数一起使用,这可能会得到相同的结果但性能更好,那么您可能只需要按 Date
排序而不是使用该函数带日期。
select
WDAYS.Date,
WDAYS.DayName,
WDAYS.WorkingDay,
sum(WDAYS.WorkingDay) OVER(PARTITION by EOMONTH(WDAYS.Date) order by Date) as 'current_working_day',
sum(WDAYS.WorkingDay) OVER(PARTITION by EOMONTH(WDAYS.Date) ) total_working_days_per_month
from WDAYS
where YEAR(WDAYS.Date) = 2022