SQL Server,运行总计,每月重置并再次求和

发布于 2025-01-17 17:58:39 字数 2297 浏览 1 评论 0原文

我有一个日历表,标记了工作日。 现在,我需要一个称为“ 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

这是我当前的输出

日期名称worktayurnatur_working_daytocun_working_days_days_per_month
2022-01-27星期四11921
2022-01-28星期五12021
20222-01 2022-012022-01-29星期六02021
2022-01 2022-30星期日0 2020 20 20 2021
2022-01-31星期一12121 21
2022-02-01星期二12220
2022-02-02星期三12320
2022-02-02-02-02星期四12420,

但列“ Current_workind_day”应该像这个

日期DayName DaynameDayname DaynameDayname Dayname Dayname Dayname current_working_daytotal_working_days_per_month
2022-01-27星期四11921
2022-01-28星期五12021
2022-01-29星期六02021
2022-01-01-01-01-30星期日02021
2022-01-012022-01-31
2022-01 -01星期二1120
2022-02-02星期三1220
2022-02-03星期四1320

感谢您的建议。

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

DateDayNameWorkingDaycurrent_working_daytotal_working_days_per_month
2022-01-27Thursday11921
2022-01-28Friday12021
2022-01-29Saturday02021
2022-01-30Sunday02021
2022-01-31Monday12121
2022-02-01Tuesday12220
2022-02-02Wednesday12320
2022-02-03Thursday12420

But the column "current_workind_day" should be like this

DateDayNameWorkingDaycurrent_working_daytotal_working_days_per_month
2022-01-27Thursday11921
2022-01-28Friday12021
2022-01-29Saturday02021
2022-01-30Sunday02021
2022-01-31Monday12121
2022-02-01Tuesday1120
2022-02-02Wednesday1220
2022-02-03Thursday1320

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅士风度i 2025-01-24 17:58:39

您可以尝试使用 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

You can try to use PARTITION by with EOMONTH function which might get the same result but better performance, then you might only need to order by Date instead of using the function with the 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文