【问题标题】:Oracle SQL: Count Weekdays of a Calendar WeekOracle SQL:计算日历周的工作日
【发布时间】:2021-05-09 18:18:00
【问题描述】:

所以我想查询某个日历周是否有全部 7 天。 如果它只返回数字 1-7 就可以了。

我的表格包含 2020 年第 3 个月的文章,但即使第一周只包含周三到周日,它仍然算作一个日历周。

通过该选择,我将制作 pl/sql 脚本来检查它,如果是的话,就会发生一些事情。

这是表格的一个例子:

Date        Articel_Id
14.10.2020  78
15.10.2020  80
16.10.2020  96
17.10.2020  100
18.10.2020  99

我可以使用 to_char() 检查日历周是否有全部 7 天吗? 如果是,如何?

【问题讨论】:

    标签: sql oracle select calendar weekday


    【解决方案1】:

    挑战实际上是定义周。如果你想使用 ISO 标准定义它们,那么聚合:

    select to_char(date, 'IYYYY-IW') as yyyyww,
           count(distinct trunc(date)) as num_days
    from t
    group by to_char(date, 'IYYYY-IW')
    order by yyyyww;
    

    这会计算每周的天数。我不确定您是否要过滤,有一个标志,或者结果集应该是什么样子。对于过滤,使用having 子句,例如having count(distinct trunc(date)) = 7

    【讨论】:

    • 我可以在其中添加“Where count(distinct trunc(angelegt_am)) = 7”吗?我试过但有一个错误.. select to_char(date, 'IYYYY-IW') as yyyyww, count(distinct trunc(date)) as num_days from date where count(distinct trunc(date)) = 7 group by to_char (日期,'IYYYY-IW')按 yyyyww 排序;
    • 没关系,我明白了 :D 非常感谢!
    • 根据 ISO-8601 的正确格式是 'IYYY-"W"IW' - IYYYY 是错误的,必须是 IYYY
    猜你喜欢
    • 2013-02-22
    • 2011-04-06
    • 1970-01-01
    • 2022-12-06
    • 2022-12-11
    • 2013-06-02
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多