希望第二高的薪水,以便通过部门明智
问题描述:
create table #t
(id int,
deptid int,
sal int)
insert into #t values (1,1,1000),(2,1,2000),(3,1,3000),(4,2,2000),(5,2,3000),(6,2,6000)
SELECT * FROM #t
预期输出:希望第二高的薪水,以便通过部门明智
id deptid sal
2 1 2000
5 2 3000
答
;with cte as
(select *, row_number() over(partition by deptid order by sal desc) as rn
from #t)
select *
from cte
where rn = 2
答
;WITH CTE
AS
(
SELECT ID, deptid, sal, ROW_NUMBER() OVER(PARTITION BY deptid ORDER BY deptid) SR FROM #t
)
SELECT ID, deptid, sal FROM CTE WHERE SR = 2
答
你需要查询
create table #t
(id int,
deptid int,
sal int)
insert into #t values (1,1,1000),(2,1,2000),(3,1,3000),(4,2,2000),(5,2,3000),(6,2,6000)
select * from (
SELECT id,deptid,sal,row_number() over(partition by deptid order by sal)rnum FROM #t
) as a
where rnum=2
等都不是代码写作服务。你有什么尝试,你卡在哪里? – HoneyBadger