I have a Table with a \"Date\" Column. I want to group by hour for a specific date.
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));
Select TO_CHAR(date,'HH24')
from table
where date = TO_DATE('20110224', 'YYYYMMDD')
group by TO_CHAR(date,'HH24')
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
You can also do this:
SELECT TRUNC(datecol, 'HH24') FROM mytable
GROUP BY TRUNC(datecol, 'HH24');