ORACLE SQL运行总量和使用窗函数daytotal(ORACLE SQL Running TO

2019-07-04 11:52发布

从EMPLOYEE表,我想记录组(雇员雇用)量,还可以每天运行总计。 输入的格式是这样的:

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

用于分组的每天没问题:

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

:如何才能使用窗口函数我有一个运行总和(在最后一栏)

Answer 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



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


文章来源: ORACLE SQL Running TOTAL and daytotal using window function