ORACLE SQL使用窗口函数运行TOTAL和daytotal

时间:2012-12-04 12:16:42

标签: sql oracle running-total

在EMPLOYEE表中,我想对记录数量(雇用员工)进行分组,并且每天都有运行的TOTAL。 输入的格式如下:

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

所需的输出:

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

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);

问题 :如何使用窗口函数运行总计(在最后一列中)

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分: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)