Oracle
 sql >> Datenbank >  >> RDS >> Oracle

So wählen Sie Datensätze gruppiert nach Tageszeit aus, einschließlich Stunden ohne Datensätze

SELECT h.hrs, NVL(Quantity, 0) Quantity
FROM (SELECT TRIM(to_char(LEVEL - 1, '00')) hrs
       FROM dual
       CONNECT BY LEVEL < 25) h
LEFT JOIN (SELECT TO_CHAR(event_date, 'HH24') AS during_hour,
                  COUNT(*) Quantity
           FROM user_activity u
           WHERE event_date BETWEEN
                 to_date('15-JUN-2010 14:00:00', 'DD-MON-YYYY HH24:MI:SS') AND
                 to_date('16-JUN-2010 13:59:59', 'DD-MON-YYYY HH24:MI:SS')
           AND event = 'user.login'
           GROUP BY TO_CHAR(event_date, 'HH24')) t
ON (h.hrs = t.during_hour)
ORDER BY h.hrs;