当前位置:网站首页>Sql: median employee salary
Sql: median employee salary
2022-06-09 08:32:00 【Jieyou grocery store Q】
Employee Table contains all employees .Employee The table has three columns : staff Id, Company name and salary .
Id Company Salary
1 A 2341
2 A 341
3 A 15
4 A 15314
5 A 451
6 A 513
7 B 15
8 B 13
9 B 1154
10 B 1345
11 B 1221
12 B 234
13 C 2345
14 C 2645
15 C 2645
16 C 2652
17 C 65
problem :
Please write SQL Query to find the median salary of each company . Challenge : Whether you can use any built-in SQL Function to solve this problem .
Id Company Salary
5 A 451
6 A 513
12 B 234
9 B 1154
14 C 2645

Solution


The difference is less than 1 Is the median
WITH data as (
SELECT
id,
company,
salary,
ROW_NUMBER() over(PARTITION by Company ORDER BY Salary desc) rn,
count(Company) over(PARTITION by company) count
from Employee
)
SELECT
id,
company,
salary
FROM data
where abs(rn - (count+1)/2) < 1

边栏推荐
- Nacos 启动报错[db-load-error]load jdbc.properties error
- Laravel cross database query
- RMAN备份概念_关于备份集(Backup Set)
- 领域驱动设计(Domain Driven Design,DDD)
- Apple wins judge dismisses iPhone Security Fraud Class Action
- RMAN备份概念_关于RMAN备份的多个拷贝
- 阿里云IOT
- RMAN备份概念_关于备份保留策略(RETENTION POLICY)
- 【TeXstudio】【1】分式dfrac,tfrac Undefined control squence错误
- First knowledge of nodejs crawler framework puppeter
猜你喜欢

Compilation_ Common instructions

Redlock red lock security debate (Part 1)

Apple wins judge dismisses iPhone Security Fraud Class Action

【天线】【1】一些名词和简单概念的解释

About Eigendecomposition

EDA open source simulation tool verilator getting started 1: installation and testing

Alibaba cloud ack pull free enterprise ACR image

Nodejs使用Net模块创建TCP服务器和客户端

阿里云ack免密拉取企业版acr镜像

Secondary development of Nacos
随机推荐
Market Research - current situation and future development trend of zirconia grinding wheel Market in the world and China
RMAN备份概念_关于映像拷贝(Image Copy)
SQL: 查找重复的电子邮箱
Elk+filebeat deployment and installation
常用windwos api
Nacos startup error [db load error]load jdbc properties error
Secondary development of Nacos
【TeXstudio】【3】较为完整的论文排版模板与bib文件引用方法
配置RMAN备份的环境_配置归档redo日志删除策略(DELETION POLICY)
If and ifnull in MySQL
extends和super的使用
3D programming mode: dependent isolation mode
Market Research - current situation and future development trend of high frequency welded finned tube Market in the world and China
Kibana:Kibana 入门 (一)
2022-2028 global nonlinear node detector industry survey and trend analysis report
Written by shellcode
48-OIDC OP配置信息端点
About Matrix Decompositions
boost1.62.0编译静态库fPIC链接问题
About Matrix Decompositions