leetcode569 員工薪水中位數

2021-09-23 13:20:59 字數 2386 閱讀 7322

employee表包含所有員工。employee表有三列:員工id,公司名和薪水。

+-----+------------+--------+

|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 |

+-----+------------+--------+

請編寫sql查詢來查詢每個公司的薪水中位數。挑戰點:你是否可以在不使用任何內建的sql函式的情況下解決此問題。

+-----+------------+--------+

|id | company | salary |

+-----+------------+--------+

|5 | a | 451 |

|6 | a | 513 |

|12 | b | 234 |

|9 | b | 1154 |

|14 | c | 2645 |

+-----+------------+--------+

select

company_rownumber.id as id,

company_rownumber.company as company,

company_rownumber.salary as salary

from

#第一部分算row_number,每個公司內部的排序

(select

id,company,

salary,

@company_no:=case when @company_name = company then @company_no+1 else 1 end as company_no,

@company_name:=company

from

(select id,company,salary from employee,(select @company_no:=0,@company_name:="") b) c

order by

company,

salary) company_rownumber

join

#第二部分算每個公司總共有多少個

(select

info.id,

info.company,

info.salary,

cntfrom

(select

id,company,

salary

from

employee) info

join

(select

company,

count(1) as cnt

from

employee

group by

company) company_cnt

on info.company = company_cnt.company) company_group_cnt

on company_rownumber.id = company_group_cnt.id

#限定每個公司的row_number在總數的一半的區間裡

where

company_no >= cnt/2

and company_no <= cnt/2+1

LeetCode 569 員工薪水中位數

建表 drop table ifexists employee create table employee id int primary keyauto increment company char salary decimal insert into employee values 1 a 234...

569 員工薪水中位數

employee 表包含所有員工。employee 表有三列 員工id,公司名和薪水。idcompany salary1a 23412a 3413a15 4a153145a 4516 a5137b 158b13 9b115410b 134511b 122112b 13413 c134514c 264...

1032 員工薪水 ZZULIOJ

題目描述 某公司規定,銷售人員工資由基本工資和銷售提成兩部分組成,其中基本工資是1500元 月,銷售提成規則如下 銷售額小於等於10000元時,按照5 提成 銷售額大於10000元但小於等於50000元時,超出10000部分按照3 提成 銷售額大於50000元時,超出50000部分按照2 提成。編寫...