我需要一些关于sql查询的帮助:
我有一个包含以下值4列的表:
Emp_Id, Emp_Name, Dept_Id, Join_Date, Salary
1, John, A, 20170101, 50000
2, David, B, 20160201, 60000
1, John, A, 20170606, 60000
如果员工属于部门' A'什么是最新的工资,如果员工在部门B,最早的薪水是多少。
尝试做这样的其他事情,但是如果员工在1个以上的部门工作,那么就不会重复工作
select salary,emp_id,join_date from employee
where emp_id in (select emp_id from employee where dept = 'A')
group by emp_id
having join_date = max(join_date)
union
select salary,emp_id,join_date from employee
where emp_id in (select emp_id from employee where dept != 'A')
group by emp_id
having join_date = min(join_date)
感谢帮助
答案 0 :(得分:0)
SELECT Salary, Emp_Id, Emp_Name, Dept_Id, Join_Date
FROM employee
WHERE Dept_Id = 'A'
AND Join_Date = ( SELECT MAX(Join_Date)
FROM employee
WHERE Dept_Id = 'A'
)
UNION ALL
SELECT Salary, Emp_Id, Emp_Name, Dept_Id, Join_Date
FROM employee
WHERE Dept_Id = 'B'
AND Join_Date = ( SELECT MIN(Join_Date)
FROM employee
WHERE Dept_Id = 'B'
)