简体   繁体   中英

SQL - To find max and min salary from emp table along with emp name?

I need to find max and min salary for each department along with emp name. Below is my sample dataset. 在此处输入图像描述

Expected output is:

在此处输入图像描述

Please share your thoughts on this

Use window functions:

SELECT DISTINCT Dept_id,
       MIN(Salary) OVER (PARTITION BY Dept_id) Min_Salary,
       FIRST_VALUE(Emp_Name) OVER (PARTITION BY Dept_id ORDER BY Salary) Min_salary_emp_name,
       MAX(Salary) OVER (PARTITION BY Dept_id) Max_Salary,
       FIRST_VALUE(Emp_Name) OVER (PARTITION BY Dept_id ORDER BY Salary DESC) Max_salary_emp_name
FROM tablename;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM