当前位置:网站首页>【mysql】用sql求中位数
【mysql】用sql求中位数
2022-06-22 12:03:00 【檀越剑指大厂】
用sql求中位数
1.建表
CREATE TABLE kwan.employee_information
(
name VARCHAR(16) PRIMARY KEY,
income INTEGER NOT NULL
);
INSERT INTO kwan.employee_information
VALUES ('桑普森', 400000);
INSERT INTO kwan.employee_information
VALUES ('迈克', 30000);
INSERT INTO kwan.employee_information
VALUES ('怀特', 20000);
INSERT INTO kwan.employee_information
VALUES ('阿诺德', 20000);
INSERT INTO kwan.employee_information
VALUES ('史密斯', 20000);
INSERT INTO kwan.employee_information
VALUES ('劳伦斯', 15000);
INSERT INTO kwan.employee_information
VALUES ('哈德逊', 15000);
INSERT INTO kwan.employee_information
VALUES ('肯特', 10000);
INSERT INTO kwan.employee_information
VALUES ('贝克', 10000);
INSERT INTO kwan.employee_information
VALUES ('斯科特', 10000);
2.中位数sql
-- 查询sql
select avg(distinct income)
from (select t1.income
from kwan.employee_information t1,
kwan.employee_information t2
group by t1.income
-- s1 的条件
having sum(case when t2.income >= t1.income then 1 else 0 end)
>= count(*) / 2.0
-- s2的条件
and sum(case when t2.income <= t1.income then 1 else 0 end)
>= count(*) / 2.0) tmp;
3.解析
- 先对同一个表进行笛卡尔积join
- 通过2个having条件筛选出符合要求的数据
- 去重结果集
- 中位数一定有一半的数大于等于中位数,一半的数小于等于中位数–关键点
边栏推荐
- V4L2像素格式及其对应的含义
- MAUI使用Masa blazor组件库
- Handling methods of ogg-01431, 01003 and 01151
- Fault tolerant heap shim applied to current process. This is usually due to previous crashes
- Struggle, programmer chapter 40 one side of the wind is full of charm, and half of the paper is charming and hates to send quiet thoughts
- XML file parsed by repo
- OGG-01431、01003、01151无奈的处理方式
- input输入框只能输入,0-100之间的数组,保留两位小数
- 重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路
- linux设置 让oracle10g自启动
猜你喜欢
随机推荐
HMS core news industry solution: let technology add humanistic temperature
When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
Invisible traffic Commander: on Urban Rail Transit Signal System
V4l2 pixel format and its corresponding meaning
getenv、setenv函数(获取和设置系统环境变量) 与 环境变量
Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
增长知识网
oracle expdp导出之query转义问题
得物技术复杂 C 端项目的重构实践
JMeter generates test reports
CAT敏捷团队教练工作坊 (Coaching Agile Teams) | 8月20日开课
OceanBase数据库助力理想汽车智能生产线 可实现30秒内自动恢复
初始transformer需要了解的一些概念
机器学习与深度学习 --- 激活函数(未完待续)
Cosmos、Polkadot
Deadlock found when trying to get lock; try restarting transaction 【MySQL死锁问题解决】
表格转换为LaTex格式
Macro definition usage and typedef and Const
Redis - 8. Persistent RDB (redis database)
翻译soem的 tutorial.txt 文件









