--关于递归累计求和
if OBJECT_ID('test')is not null
drop table test
go
create table test(
id int identity(1,1),
deptid char(3),
empid char(4),
salary int
)
go
insert test(deptid,empid,salary)
select '101','1001',3500 union all
select '101','1002',2200 union all
select '102','1003',1900 union all
select '102','1004',5600 union all
select '102','1005',8000 union all
select '101','1006',2400 union all
select '101','1007',2300 union all
select '103','1008',3200 union all
select '103','1009',7800 union all
select '104','1010',4500 union all
select '101','1011',6500 union all
select '104','1012',3500 union all
select '104','1013',1900 union all
select '103','1014',2700 union all
select '102','1015',3100 union all
select '104','1016',2600
go
--problem 1:求出所有员工的工资的累计(从工资的最高到最低累计)
;with t
as(
select
px=ROW_NUMBER()over(order by salary desc),
deptid,empid,salary
from test
),
m as(
select px,deptid
SQL使用递归实现数据的累加
最新推荐文章于 2022-05-12 18:27:27 发布