表:员工
empid depti salary hired_date
1 1 4000 2012-01-01
2 1 3000 2015-11-01
3 1 3000 2015-01-11
4 2 4000 2015-11-01
5 1 3000 2014-11-01
6 2 5000 2015-01-01
查询:
select emp1.empid, emp1.salary as higher, emp2.empid, emp2.salary as lower_sal
from employee as emp1, employee as emp2
where emp1.salary >= 4000 and emp2.salary < 4000
group by emp1.empid, emp2.empid, emp1.salary, emp2.salary
以上查询结果:
empid higher empid lower_sal
1 4000 2 3000
1 4000 3 3000
1 4000 5 3000
4 4000 2 3000
4 4000 3 3000
4 4000 5 3000
6 5000 2 3000
6 5000 3 3000
6 5000 5 3000
预期结果:
empid higher empid lower_sal
1 4000 2 3000
4 4000 3 3000
6 5000 5 3000
需要在查询中包含哪些内容才能获得此输出并避免重复值。
答案 0 :(得分:0)
你尝试过使用distinct吗?这是您可以添加的子句,表示您不需要重复值。在“选择”之后添加。
select distinct emp1.empid, emp1.salary as higher, emp2.empid, emp2.salary as lower_sal
from employee as emp1, employee as emp2
where emp1.salary >= 4000 and emp2.salary < 4000
group by emp1.empid, emp2.empid, emp1.salary, emp2.salary
答案 1 :(得分:0)
根据我的理解,您可以使用交叉申请,
SELECT *
FROM Employee A
CROSS APPLY (SELECT Min(B.Salary) AS Min_Salary
FROM Employee B
WHERE B.salary < 4000)B
WHERE A.Salary >= 4000
答案 2 :(得分:0)