当前位置:网站首页>[MySQL] using SQL to find the median
[MySQL] using SQL to find the median
2022-06-22 12:32:00 【Tanyue Jianzhi Dachang】
use sql Find the median
1. Build table
CREATE TABLE kwan.employee_information
(
name VARCHAR(16) PRIMARY KEY,
income INTEGER NOT NULL
);
INSERT INTO kwan.employee_information
VALUES (' Sampson ', 400000);
INSERT INTO kwan.employee_information
VALUES (' mike ', 30000);
INSERT INTO kwan.employee_information
VALUES (' white ', 20000);
INSERT INTO kwan.employee_information
VALUES (' Arnold ', 20000);
INSERT INTO kwan.employee_information
VALUES (' Smith. ', 20000);
INSERT INTO kwan.employee_information
VALUES (' Laurence ', 15000);
INSERT INTO kwan.employee_information
VALUES (' Hudson ', 15000);
INSERT INTO kwan.employee_information
VALUES (' Kent ', 10000);
INSERT INTO kwan.employee_information
VALUES (' Baker, ', 10000);
INSERT INTO kwan.employee_information
VALUES (' Scott ', 10000);
2. Median sql
-- Inquire about sql
select avg(distinct income)
from (select t1.income
from kwan.employee_information t1,
kwan.employee_information t2
group by t1.income
-- s1 Conditions
having sum(case when t2.income >= t1.income then 1 else 0 end)
>= count(*) / 2.0
-- s2 Conditions
and sum(case when t2.income <= t1.income then 1 else 0 end)
>= count(*) / 2.0) tmp;
3. analysis
- First, Cartesian product the same table join
- adopt 2 individual having Conditions filter out the data that meets the requirements
- De duplication result set
- Half of the median must be greater than or equal to the median , Half the number is less than or equal to the median – Key points
边栏推荐
- oracle存储过程2
- 增长知识网
- input输入框只能输入,0-100之间的数组,保留两位小数
- Fight, programmer chapter 39 how can you admire a bosom friend if you are not disappointed in life
- Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
- OceanBase数据库助力理想汽车智能生产线 可实现30秒内自动恢复
- Small demo of QT page layout class
- ONNX调研
- Linux下启动oracle
- 银行支付接口开发之子账户
猜你喜欢

TIS教程03-导出

Sliding conflict handling effect of cloud music imitating Netease

第十一届 蓝桥杯 嵌入式设计与开发项目 决赛
![[cloud native | kubernetes] Introduction to kubernetes (I)](/img/e0/c75e72fc3636b3de8f5caded19a883.png)
[cloud native | kubernetes] Introduction to kubernetes (I)

TIS教程01-安装

TIS教程02-模型

LDA学习笔记

Flutter——实现网易云音乐的渐进式卡片切换

Vue mixed write Minxin, virtual dom/ref, dynamic components, cache keep

1961-Check If String Is a Prefix of Array(检查字符串是否为数组前缀)
随机推荐
运筹优化基础知识
Messari年度报告-2021
4tb production database cannot be accessed due to disk rejecting i/o to offline device failure
OceanBase数据库助力理想汽车智能生产线 可实现30秒内自动恢复
sql行列转换
V4l2 pixel format and its corresponding meaning
Andrdoid延时操作
深入解析Glide源码
SQL Server到Oracle连接服务器的实现_包括查询的实现
Could not determine which parameter to assign generated keys to
职场杰出人士的20个好习惯
Word技巧汇总
Machine learning and deep learning - activation function (to be continued)
Flutter——实现网易云音乐的渐进式卡片切换
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
Getenv, setenv functions (get and set system environment variables) and environment variables
MAUI使用Masa blazor组件库
Yunshang people and IOT technology joined the dragon lizard community to jointly create a software and hardware service ecosystem
【mysql】用sql求中位数
Error: unresolved variable $bus and "typeerror: cannot read property '$on' of undefined“