针对不同条件进行聚合

  • 本文关键字:条件 sql presto
  • 更新时间 :
  • 英文 :


我有一个窗口功能如下:

select d.Name as Department, a. Name as Employee, a.sum(salary) 
from ( select e.*, dense_rank() over (partition by DepartmentId order by Salary desc) as DeptPayRank 
from Employee e 
) a 
join Department d
on a. DepartmentId = d. Id 
where DeptPayRank =1 
group by 1,2

如果我还想拿所有工资的最小值,我可以添加到此查询中DepPayRank = 3,或者不可能?

测试:

SELECT d.Name AS Department, 
a.Name AS Employee, 
SUM(CASE WHEN DeptPayRank = 1 THEN salary END),
MIN(CASE WHEN DeptPayRank = 3 THEN salary END)
FROM ( SELECT e.*, 
DENSE_RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) AS DeptPayRank 
from Employee e 
) a 
JOIN Department d ON a.DepartmentId = d.Id 
GROUP BY 1,2
/* HAVING COUNT(CASE WHEN DeptPayRank = 1 THEN 1 END) > 0 */

相关内容

  • 没有找到相关文章

最新更新