赞
踩
select com_id, floor((count(salary)+1)/2) as start,
floor((count(salary)+2)/2) as end
from employee group by com_id order by com_id
select com_id,salary
from
(
select com_id,
salary,
row_number() over(partition by com_id order by salary desc) as rnk,
count(salary) over(partition by com_id) as num
from employee
) t1
where t1.rnk in (floor(num/2)+1, if(mod(num,2)=0,floor(num/2),floor(num/2)+1)
order by com_id
select com_id, salary
from
(
select com_id,salary,
row_number() over(partition by com_id order by salary Desc) rnk,
count(salary) over(partition by com_id) as num
from employee
) t1
where abs(t1.rnk - (t1.num+1)/2) < 1
order by com_id
注意:不可在一次查询中对窗口函数的结果进行操作
因为查询的顺序为:from->where->group by->having->select->order by
select com_id,salary
from
(
select *,
row_number() over(partition by com_id order by salary) as rnk1,
row_number() over(partition by com_id order by salary desc) as rnk2
from employee
) t1
where rnk1=rnk2 or abs(rnk1-rnk2)=1
order by com_id
报错:BIGINT UNSIGNED value is out of range
两种方式修改:
直接修改设置SET sql_mode=‘NO_UNSIGNED_SUBTRACTION’
或者修改代码
select com_id,salary
from
(
select *,
row_number() over(partition by com_id order by salary) as rnk1,
row_number() over(partition by com_id order by salary desc) as rnk2
from employee
) t1
where t1.rnk1 = t1.rnk2 or abs(cast(t1.rnk1 as signed)-cast(t1.rnk2 as signed)) = 1
ref:SQL 求中位数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。