【问题标题】:How to get the rows that is in the same week?如何获取同一周内的行?
【发布时间】:2012-10-11 12:53:22
【问题描述】:

我有一个数据库和一个数据库中的日期列,其值类似于 2012 年 5 月 2 日,而我试图获取同一周内的所有行并计算每周它有多少行。 我尝试得到如下输出:

例如

8-14 October  |  15
1-7 October   |  8
24-30 September | 12
etc...

我可能会使用 group by 和 count 语句和函数,但我不知道如何检测像 10 月 8 日至 14 日这样的一周。

数据库是oracle 的maximo 数据库。谢谢。

【问题讨论】:

  • 哪个 RDBMS ? mysql还是sql server?
  • 对不起,这是一个oracle数据库。

标签: sql database oracle date group-by


【解决方案1】:

Trunc(date_value) 将返回一周的第一天(星期日):

select 
    trunc(date_column,'D') as start_date, 
    trunc(date_column,'D')+6 end_date, 
    count(*)
from your_table
group by trunc(date_column,'D')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多