user_id salary month
1 100 1
1 150 2
1 200 3
1 180 4
1 140 5
2 10 1
2 40 2
2 20 3
2 15 4
2 45 5
我想選擇前2的薪水每個人。
我嘗試瞭解交叉應用。看起來像我發明的任務符合交叉應用。
現在我有以下查詢
select distinct(s.user_id) from Salary s
cross apply (
select top 2 * from Salary sal
order by sal.salary desc
)sal
的樣子,我從預期的結果遠遠不夠。
預期的結果:
1 180
1 200
2 40
2 45
喜歡這個? http://stackoverflow.com/questions/176964/select-top-10-records-for-each-category – DavidG