我嘗試從頂部5名員工獲得所有3最高的薪水這樣第三高的薪水SQL
salary
35000
34000
20000
12000
500
40000
25000
41000
90000
550000
查詢
select top 5
(SELECT MAX(grosssalary) FROM Detail) maxsalary ,
(SELECT MAX(grosssalary) FROM Detail) sec_max_salary,
(SELECT MAX(grosssalary) FROM Detail
WHERE grosssalary NOT IN (SELECT MAX(grosssalary) FROM Detail)) as third_max_salary
但這樣表示
maxsalary sec_max_salary third_max_salary
550000 550000 41000
,其中數據因爲我想要這樣的數據
maxsalary sec_max_salary third_max_salary
550000 90000 41000
可能的重複:http://stackoverflow.com/questions/16234983/how-to-find-third-or-nth-maximum-salary-from -salary- – thebenman
可以檢查我的答案@superuser – Chanukya
http://stackoverflow.com/q/16234983/3261852 – Munavvar