2017-07-27 31 views
-1

下面的查詢:SQL查詢有關薪水最高找到

SELECT ROW_NUMBER() OVER(ORDER BY sum(salary) DESC) as num,id, sum(salary) as salary 
FROM person 
GROUP BY id 
where num=3; 

是給我一個錯誤在where條件,我想第3點的數據...什麼是該解決方案?

+0

WHERE過濾器聚合之前,您需要具有後對結果進行過濾聚合。 –

回答

0

你需要一個子查詢:

select p.* 
from (select ROW_NUMBER() OVER (ORDER BY sum(salary) DESC) as num, id, 
      sum(salary) as salary 
     from person 
     group by id 
    ) p 
where num = 3; 

(當然,除非你正在使用Teradata和可以使用qualify條款。)

相關問題