SQL group by date (hour)

后端 未结 4 832
鱼传尺愫
鱼传尺愫 2021-02-01 06:33

I have a Table with a \"Date\" Column. I want to group by hour for a specific date.

相关标签:
4条回答
  • 2021-02-01 06:49

    Alternatively, I would have suggested EXTRACT (datetime), but (empahsis mine):

    If HOUR, MINUTE, or SECOND is requested, then expr must evaluate to an expression of data type TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE, or INTERVAL DAY TO SECOND. DATE is not valid here, because Oracle Database treats it as ANSI DATE data type, which has no time fields.

    Therefore, the result is a little bit ugly:

      SELECT EXTRACT (HOUR FROM CAST (m.a_date AS TIMESTAMP)) AS hour_,
             COUNT (*) AS count_
        FROM MY_TABLE m
    GROUP BY EXTRACT (HOUR FROM CAST (m.a_date AS TIMESTAMP));
    
    0 讨论(0)
  • 2021-02-01 06:55
    Select TO_CHAR(date,'HH24')
    from table
    where date = TO_DATE('20110224', 'YYYYMMDD')
    group by TO_CHAR(date,'HH24')
    
    0 讨论(0)
  • 2021-02-01 07:00
    select to_char(datecol,'HH24') thehour, count(*) count_in_hour
    from tbl
    where datecol = date '20110224'
    group by to_char(datecol,'HH24')
    order by thehour asc
    
    0 讨论(0)
  • 2021-02-01 07:10

    You can also do this:

    SELECT TRUNC(datecol, 'HH24') FROM mytable
    GROUP BY TRUNC(datecol, 'HH24');
    
    0 讨论(0)
提交回复
热议问题