with t as (
select id, company, salary, row_number() over(partition by company order by salary) as 'rk', count(id) over(partition by company) as 'cnt'
from Employee
)
select id, company, salary
from t
where rk >= cnt / 2 and rk <= cnt / 2 + 1
不算难吧,如果用函数的话,很有意思的事,中位数这个问题通过cnt和rank的大小来决定是一个还是两个,这个点想到了应该没什么难度

这篇博客探讨了一种使用SQL查询计算公司内部各个部门员工薪资中位数的方法。通过窗口函数row_number()和count(),结合条件筛选,可以有效地找出每个部门薪资的中位数,无论是单值还是范围。这种方法对于理解和处理大数据集中的统计问题非常有用。
8987

被折叠的 条评论
为什么被折叠?



