02 May 2008

Grouping Records by Hours

Following query will return the records grouped by every hour

SELECT   TRUNC (action_time, 'HH24') hours,

         COUNT (TO_CHAR (action_time, 'HH24')) RECORDS

    FROM pfl_performance_logs

   WHERE action_time BETWEEN TO_DATE ('02/11/2007 00:00:00', 'DD/MM/YYYY HH24:MI:SS')

                      AND TO_DATE ('11/01/2008 00:00:00', 'DD/MM/YYYY HH24:MI:SS')

GROUP BY TRUNC (action_time, 'HH24')

 
Also, we can use the above query to group records on day (DDD, DD), month (month, mon, mm), year (YYYY, YY), quarter (Q), week (WW, IW) etc.
Enjoy QUERYING