与最高工资查找相关的SQL查询

与最高工资查找相关的SQL查询,sql,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; 在中给我一个错误,我想获取第3行数据。。。此问题的解决方案是什么?您需要一个子查询: select p.* from (select ROW_NUMBER() OVER (ORDER BY sum(salary) DESC) as num,

以下查询:

SELECT ROW_NUMBER() OVER(ORDER BY sum(salary) DESC) as num,id, sum(salary) as salary
FROM person
GROUP BY id
where num=3;
在<代码>中给我一个错误,我想获取第3行数据。。。此问题的解决方案是什么?

您需要一个子查询:

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
子句。)

如果在聚合之前进行筛选,则需要在聚合之后对结果进行筛选。