What you are looking for is a turning point - wrapping row data into columns.
Oracle 9i + using WITH / CTE:
Using:
WITH summary AS ( SELECT TRUNC(ls.started,'HH') AS dt, ls.depot, COUNT(*) AS num_depot FROM logstats ls GROUP BY TRUNC(ls.started,'HH'), ls.depot) SELECT s.dt, MAX(CASE WHEN s.depot = 'foo' THEN s.num_depot ELSE 0 END) AS "count_of_foo", MAX(CASE WHEN s.depot = 'bar' THEN s.num_depot ELSE 0 END) AS "count_of_bar" FROM summary s GROUP BY s.dt ORDER BY s.dt
Equivalent Non-WITH / CTE
Using:
SELECT s.dt, MAX(CASE WHEN s.depot = 'foo' THEN s.num_depot ELSE 0 END) AS "count_of_foo", MAX(CASE WHEN s.depot = 'bar' THEN s.num_depot ELSE 0 END) AS "count_of_bar" FROM (SELECT TRUNC(ls.started,'HH') AS dt, ls.depot, COUNT(*) AS num_depot FROM LOGSTATS ls GROUP BY TRUNC(ls.started, 'HH'), ls.depot) s GROUP BY s.dt ORDER BY s.dt
Previously, Oracle9i needs CASE statements changed to DECODE , Oracle-specific IF / ELSE logic.
Oracle 11g + using PIVOT
Unverified:
SELECT * FROM (SELECT TRUNC(ls.started, 'HH') AS dt, ls.depot FROM LOGSTATS ls GROUP BY TRUNC(ls.started, 'HH'), ls.depot) PIVOT ( COUNT(*) FOR depot ) ORDER BY 1
source share