我有一个名为departments的表,其中包含一个名为salary的列。现在我想了解每个部门最高两个薪水的所有细节。我们应该如何开发一个产生所需输出的查询?前N个分析将作为一个整体给出,但不是每个部门。我想要每个部门的前两名。
答案 0 :(得分:5)
我意识到你在Oracle中要求这样做。我无法帮助你。
但是,如果您在MSSQL / TSQL中看到解决方案,它可能会有所帮助吗?
select
d.Salary
,d.Department
from
(
select
r.Salary
,r.Department
,row_number() over(
partition by r.Department
order by r.Salary desc) as RowNumber
from HumanResources as r
) as d
where d.RowNumber < 3
祝你好运!
答案 1 :(得分:1)
DECLARE @TV_SAL TABLE (EMPID INT, DEPTID CHAR(10),SAL INT)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(4,'OR',1004)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(5,'OR',1005)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(1,'OR',1001)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(2,'OR',1002)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(17,'CS',1503)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(18,'CS',1503)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(14,'CS',1500)
INSERT INTO @TV_SAL(EMPID,DEPTID,SAL) VALUES(15,'CS',1501)
SELECT *
FROM @TV_SAL A
WHERE ( SELECT COUNT(DISTINCT(SAL))
FROM @TV_SAL B
WHERE A.SAL <= B.SAL
AND A.DEPTID = B.DEPTID
) <= 3 -- Replace this with 1 ,2 or n , n indicates top n
ORDER BY DEPTID, SAL DESC
答案 2 :(得分:-2)
SELECT TOP 5 b.DepartName,a.Salary FROM Employee a
加入
部门b ON a.DepartId = b.DepartId
按b.DepartName分组ORDER BY a.Salary DESC
它在Sql服务器上运行正常,我不知道Oracle