SQL计数器与条件

huangapple go评论49阅读模式
英文:

SQL Counter with condition

问题

我在SQL表中存储了一个年度日历,每天(日期)都有两个标记:假日和周末。
当特定日期是假日时,假日条目为'1'
当特定日期是周末时,周末条目为'1'
当特定日期既是假日又是周末时,假日和周末条目都为'1'

现在,我正在尝试做以下事情:

  1. 计算每个月的工作日数量
  2. 如果日期是假日、周末或两者都是,那一天的计数应该等于前一天的日期

以下是示例:

日期 年份 月份 是否周末 是否假日 计数
2023.01.01 2023 1 1 1 1 0
2023.01.02 2023 1 2 0 1 0
2023.01.03 2023 1 3 0 0 1
2023.01.04 2023 1 4 0 0 2
2023.01.05 2023 1 5 0 0 3
2023.01.06 2023 1 6 0 0 4
2023.01.07 2023 1 7 1 0 4
2023.01.08 2023 1 8 1 0 4
2023.01.09 2023 1 9 0 0 5
2023.01.10 2023 1 10 0 0 5

到目前为止,我尝试过使用ROW_NUMBER()和设置@Counter,但没有成功。

英文:

I have a yearly calendar stored in an sql table amd each day (date) has two markers: Holiday & Weekend.
When the specific date is a holiday, the Holiday entry is '1'
When the specific date is a weekend, the Weekend entry is '1'
When the specific date is a holiday & weekend, both Holiday and Weekend entries are '1'

Now, what I am trying to do is the followig:

  1. Count number of working days in each month
  2. if date is holiday, weekend or both, the count of that day should be equal to the date before

Below is an example:

Date Year Month Day IsWeekend IsHoliday Counter
2023.01.01 2023 1 1 1 1 0
2023.01.02 2023 1 2 0 1 0
2023.01.03 2023 1 3 0 0 1
2023.01.04 2023 1 4 0 0 2
2023.01.05 2023 1 5 0 0 3
2023.01.06 2023 1 6 0 0 4
2023.01.07 2023 1 7 1 0 4
2023.01.08 2023 1 8 1 0 4
2023.01.09 2023 1 9 0 0 5
2023.01.10 2023 1 10 0 0 5

So far, I have tried ROW_NUMBER() and setting a @Counter but with no luck

答案1

得分: 1

COUNT(CASE WHEN IsHoliday = 0 AND IsWeekend = 0 THEN 1 END) OVER(ORDER BY "Date"):

在"Date"字段排序的情况下,统计IsHoliday等于0且IsWeekend等于0的数量。

英文:
COUNT(CASE WHEN IsHoliday = 0 AND IsWeekend = 0 THEN 1 END) OVER(ORDER BY "Date")

huangapple
  • 本文由 发表于 2023年3月9日 20:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定