SQL Server,运行总计,每月重置并再次求和
我有一个日历表,标记了工作日。 现在,我需要一个称为“ 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
这是我当前的输出
日期 | 名称 | worktay | urnatur_working_day | tocun_working_days_days_per_month |
---|---|---|---|---|
2022-01-27 | 星期四 | 1 | 19 | 21 |
2022-01-28 | 星期五 | 1 | 20 | 21 |
20222-01 2022-01 | 2022-01-29星期六 | 0 | 20 | 21 |
2022-01 2022-30 | 星期日 | 0 20 | 20 20 20 20 | 21 |
2022-01-31 | 星期一 | 1 | 21 | 21 21 |
2022-02-01 | 星期二 | 1 | 22 | 20 |
2022-02-02 | 星期三 | 1 | 23 | 20 |
2022-02-02-02-02 | 星期四 | 1 | 24 | 20, |
但列“ Current_workind_day”应该像这个
日期 | DayName Dayname | Dayname Dayname | Dayname Dayname Dayname Dayname current_working_day | total_working_days_per_month |
---|---|---|---|---|
2022-01-27 | 星期四 | 1 | 19 | 21 |
2022-01-28 | 星期五 | 1 | 20 | 21 |
2022-01-29 | 星期六 | 0 | 20 | 21 |
2022-01-01-01-01-30 | 星期日 | 0 | 20 | 21 |
2022-01-01 | | 2022-01-31 | | |
2022-01 -01 | 星期二 | 1 | 1 | 20 |
2022-02-02 | 星期三 | 1 | 2 | 20 |
2022-02-03 | 星期四 | 1 | 3 | 20 |
感谢您的建议。
I have a calendar table where working days are marked.
Now I need a running total called "current_working_day" which sums up the working days until the end of a month and restarts again.
This is my query:
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
This is my current output
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 |
But the column "current_workind_day" should be like this
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 |
Thanks for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用
eomonth
函数使用分区,该功能可能会得到相同的结果,但性能更好,那么您可能只需要按
date
订购。而不是与日期一起使用该函数。You can try to use
PARTITION by
withEOMONTH
function which might get the same result but better performance, then you might only need to order byDate
instead of using the function with the date.