SQL group by date (hour)

后端 未结 4 833
鱼传尺愫
鱼传尺愫 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));
    

提交回复
热议问题