标签:creat order by rom 函数 输出 asc 字段 结果 ota
按照salary的累计和running_total,其中running_total为前N个当前( to_date = ‘9999-01-01‘)员工的salary累计和,其他以此类推。 具体结果如下Demo展示。。
CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
输出格式:
1.函数+OVER (ORDER BY 字段)
select emp_no,salary,sum(salary) OVER (ORDER BY emp_no) as running_total
from salaries
2.分组法(还要细细体会)
select a.emp_no, a.salary, sum(b.salary)
from salaries as a, salaries as b
where b.emp_no <= a.emp_no
and a.to_date = ‘9999-01-01‘
and b.to_date = ‘9999-01-01‘
group by a.emp_no
order by a.emp_no asc
标签:creat order by rom 函数 输出 asc 字段 结果 ota
原文地址:https://www.cnblogs.com/liuxiangyan/p/14394567.html