当前位置:网站首页>Solution | get the relevant information about the current employees' highest salary in each department |
Solution | get the relevant information about the current employees' highest salary in each department |
2022-07-29 04:58:00 【StackChan】
Source of problem :
sql Difficult questions :SQL12: Get information about the highest paid employees in each department
difficulty : Solve and search the aggregation process ,sql need select Non aggregate Columns
Solutions : Apply two sub tables to query , An aggregation result for the aggregation column ( Non aggregate column ), One contains Unaggregated results of aggregated columns and Non aggregate Columns
***Mysql And Sqlite select Non aggregate Columns *** The result is Not the same , If this question only requires output :
dept_no,maxSalary Two , Only use max Aggregate functions :
select de.dept_no,max(sa.salary) as maxSalary
from dept_emp as de left join salaries as sa
on de.emp_no=sa.emp_no
group by de.dept_no
order by de.dept_no asc
But ontology needs select emp_no This non aggregated field , And an aggregate field (dept_no) Corresponding to multiple non aggregated fields (emp_no), So when you choose ,Sqlite Any one of the non aggregated fields will be randomly selected ( Not choice max(sa.salary) The one corresponding to the maximum salary emp_no), and mysql Will report an error directly !
This is a select Non aggregate fields , Classical problems !!!
resolvent :
First construct a sub table t1( The salary value of the Contract Department ), Used to find the maximum salary max(sa.salary) With its dept_no,
(
select de.dept_no,max(sa.salary) as salary
from dept_emp as de left join salaries as sa
on de.emp_no=sa.emp_no
group by dept_no
) as t1
Then build a sub table t2( Do not aggregate the salary value of the same department in the table , Merge only two tables ), Storage dept_no,salary(t2 Medium dept_no,salary For and t1 Medium dept_no,salary comparison ),emp_no
(
select de.dept_no,de.emp_no,sa.salary
from dept_emp as de left join salaries as sa
on de.emp_no=sa.emp_no
) as t2
t1,t2 Joint query of two sub tables :
select t2.dept_no,t2.emp_no,t1.salary as maxSalary from
(
select de.dept_no,max(sa.salary) as salary
from dept_emp as de left join salaries as sa
on de.emp_no=sa.emp_no
group by dept_no
) as t1 left join
(
select de.dept_no,de.emp_no,sa.salary
from dept_emp as de left join salaries as sa
on de.emp_no=sa.emp_no
) as t2
on t1.dept_no=t2.dept_no and t1.salary=t2.salary
order by t2.dept_no asc

边栏推荐
- C language implementation of three chess
- Delete blank pages in word documents
- Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
- Correct user dragging method
- How does excel filter out the content you want? Excel table filtering content tutorial
- P5714 [deep foundation 3. Case 7] obesity
- Original code, inverse code, complement code
- WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
- 如何避免示波器电流探头损坏
- 【微信小程序】swiper滑动页面,滑块左右各露出前后的一部分,露出一部分
猜你喜欢

What if the office prompts that the system configuration cannot run?

Office提示系统配置无法运行怎么办?

How to set row height and column width in excel? The method of setting row height and column width in Excel

Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format

如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然

Ethernet of network

Opencv learning 1 (environment configuration)

Command line interactive tools (latest version) inquirer practical tutorial

C语言实现三子棋
![学术 | [LaTex]超详细Texlive2022+Tex Studio下载安装配置](/img/4d/f8c60c0fbbd98c4da198cfac7989fa.png)
学术 | [LaTex]超详细Texlive2022+Tex Studio下载安装配置
随机推荐
IOS interview preparation - other articles
Reveal安装配置调试
JS daily question (11)
Command line interactive tools (latest version) inquirer practical tutorial
Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
Opencv learning 1 (environment configuration)
Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
Download addresses of various versions of MySQL and multi version coexistence installation
WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!
What if the computer cannot open excel? The solution of Excel not opening
【无标题】
Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
Live in small private enterprises
输入的查询SQL语句,是如何执行的?
How to build a mobile studio network?
How to avoid damage of oscilloscope current probe
P1009 [noip1998 popularization group] sum of factorials
2021-10-23
电脑无法打开excel表格怎么办?excel打不开的解决方法
iOS面试准备 - 其他篇