ORACLE SQL Running TOTAL and daytotal using window function

user1875323 picture user1875323 · Dec 4, 2012 · Viewed 19.9k times · Source

From the EMPLOYEE table, I want to group the amount of records(employees hired) AND also have the running TOTAL per day. The format of the input is like this:

rownum  Hired_date_time
1       1/10/2012 11:00
2       1/10/2012 13:00
3       20/11/2012 10:00
4       20/11/2012 15:00
5       20/11/2012 16:00
6       30/12/2012 1:00

The desired output:

Hired_date.......Hired_per_day.........TOTAL_number_of_employees
1/10/2012 ...................2 ........2
20/11/2012 ..................3 ........5
30/12/2012 ..................1 ....... 6

No problem for the GROUPING PER DAY:

select  trunc(Hired_date_time) as "Hired_date" , 
        count(*) as "Hired_per_day"
from employee
group by trunc(Hired_date_time)
order by trunc(Hired_date_time);

Question: how can I have a running total (in last column) using the window function

Answer

Kirill Leontev picture Kirill Leontev · Dec 4, 2012
select trunc(hired), 
       count(*) hired_today,       
       sum(count(*)) over (order by trunc(hired)) as running_total
from emp
group by trunc(hired)

http://sqlfiddle.com/#!4/4bd36/9