ORACLE SQL Running TOTAL and daytotal using window function

不羁岁月 提交于 2019-11-30 18:35:27

问题


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


回答1:


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




回答2:


select trunc(hire_date), 
       count(*) over (partition by trunc(hire_date)) as hired_per_day,
       count(*) over (order by hire_date) as total_number_of_employees
from employee
order by trunc(hire_date)


来源:https://stackoverflow.com/questions/13702693/oracle-sql-running-total-and-daytotal-using-window-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!