Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL group by date (hour)

Tags:

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

like image 328
kal Avatar asked Feb 24 '11 22:02

kal


People also ask

How to group by hours in SQL?

When you want to group by minute, hour, day, week, etc., you may be tempted to just group by your timestamp column. The datepart() function has the same syntax as the datename() function. Therefore, both functions can be used in the same way.

Can you group by multiple columns in SQL?

We can use the group by multiple column technique to group multiple records into a single record. All the records that have the same values for the respective columns mentioned in the grouping criteria can be grouped as a single column using the group by multiple column technique.


2 Answers

You can also do this:

SELECT TRUNC(datecol, 'HH24') FROM mytable GROUP BY TRUNC(datecol, 'HH24'); 
like image 181
Jeffrey Kemp Avatar answered Oct 05 '22 23:10

Jeffrey Kemp


Select TO_CHAR(date,'HH24') from table where date = TO_DATE('20110224', 'YYYYMMDD') group by TO_CHAR(date,'HH24') 
like image 24
rene Avatar answered Oct 05 '22 23:10

rene