建表
drop table if EXISTS employee;
create table employee
(
Id int primary key auto_increment,
Company char,
Salary DECIMAL
);
insert into employee values(1, 'A', 2341);
insert into employee values(null, 'A', 341);
insert into employee values(null, 'A', 15);
insert into employee values(null, 'A', 15314);
insert into employee values(null, 'A', 451);
insert into employee values(null, 'A', 513);
insert into employee values(null, 'B', 15);
insert into employee values(null, 'B', 13);
insert into employee values(null, 'B', 1154);
insert into employee values(null, 'B', 1345);
insert into employee values(null, 'B', 1221);
insert into employee values(null, 'B', 234);
insert into employee values(null, 'C', 2345);
insert into employee values(null, 'C', 2645);
insert into employee values(null, 'C', 2645);
insert into employee values(null, 'C', 2652);
insert into employee values(null, 'C', 65);
首先明确:
中位数,位置在最中间的数
中位数的位置:
当样本数为奇数时:中位数为第(N+1)/2个数据
当样本数为偶数时:中位数为第N/2个数据与第N/2+1个数据的算术平均值
偶数时,根据题意,N/2和N/2+1的数都取到了
算出每个公司的人数、薪水的排序where 选择,根据人数奇偶筛选
select Id, Company, Salary
from
(
select *, row_number() over (partition by Company order by Salary) rnk,
count(*) over (partition by Company) num
from Employee
) t
where(
-- (num%2=1 and rnk = floor(num/2)+1)
(num%2=1 and rnk = (num+1)/2)
or
-- (num%2=0 and (rnk = floor(num/2) or rnk = floor(num/2)+1))
(num%2=0 and (rnk = num/2 or rnk = (num/2)+1))
)
原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说
免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。
您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com未经允许不得转载:书荒源码初雪模板-源码网每日更新网站源码模板! » LeetCode–569. 员工薪水中位数
关注我们小说电影免费看关注我们,获取更多的全网素材资源,有趣有料!120000+人已关注
评论抢沙发