Select 条出现不止一次的记录
Select records that appear more than once
我正在尝试 select 出现不止一次并且属于特定部门和其他部门的记录。
到目前为止,我的查询是这样的:
SELECT employeeCode, employeeName
FROM
Employees
WHERE
Department <> 'Technology'
AND employeeCode IN (SELECT employeeCode
FROM Employees
GROUP BY employeeCode HAVING COUNT(*) > 1)
问题是我想 select 属于技术部门的员工,但他们也参与其他部门。
所以,他们肯定是科技部的,但也有可能是家居部的。在数据库中它可能看起来像:
1 | A1 | Alex | Technology
2 | A2 | Thor | Household
3 | A3 | John | Cars
4 | A3 | John | Technology
5 | A4 | Kim | Technology
6 | A4 | Kim | Video Games
所以基本上查询应该 return:
A3 | John |
A4 | Kim |
我认为这是我遗漏的一小部分但是..
关于如何 filter/sort 它始终使用该技术和其他部门的任何想法?
顺便说一句,我尝试搜索但找不到像我这样的问题..
试试这个:
SELECT E.employeeCode, E.employeeName
FROM Employees E
INNER JOIN (SELECT DISTINCT E1.employeeCode, E1.employeeName
FROM Employees E
WHERE E.Department = 'Technology'
) AS A ON E.employeeCode = A.employeeCode AND E.employeeName = A.employeeName
GROUP BY E.employeeCode, E.employeeName
HAVING COUNT(*) > 1;
如果您想要技术部门和其他部门的员工:
select e.employeeCode, e.employeeName
from employees e
group by e.employeeCode, e.employeeName
having sum(case when e.department = 'Technology' then 1 else 0 end) > 0 and
count(*) > 1;
假设 table 中没有重复项。如果它可以有重复项,则使用 count(distinct department) > 1
而不是 count(*) > 1
.
您可以在具有不同条件的相同 table 上将 EXISTS 与关联子查询结合使用。
SELECT e1.employeeCode, e1.employeeName
FROM Employees e1
WHERE e1.Department = 'Technology'
AND EXISTS (SELECT * FROM Employees e2
WHERE e1.employeeCode = e2.employeeCode
AND e2.Department <> 'Technology')
这适用于您的情况:
SELECT a.employeeCode, a.employeeName
FROM Employees a, Employees b
WHERE
a.Department = 'Technology'
AND
b.Department <> 'Technology'
AND
a.employeeCode = b.employeeCode
AND
a.employeeID <> b.employeeID
我正在尝试 select 出现不止一次并且属于特定部门和其他部门的记录。
到目前为止,我的查询是这样的:
SELECT employeeCode, employeeName
FROM
Employees
WHERE
Department <> 'Technology'
AND employeeCode IN (SELECT employeeCode
FROM Employees
GROUP BY employeeCode HAVING COUNT(*) > 1)
问题是我想 select 属于技术部门的员工,但他们也参与其他部门。
所以,他们肯定是科技部的,但也有可能是家居部的。在数据库中它可能看起来像:
1 | A1 | Alex | Technology
2 | A2 | Thor | Household
3 | A3 | John | Cars
4 | A3 | John | Technology
5 | A4 | Kim | Technology
6 | A4 | Kim | Video Games
所以基本上查询应该 return:
A3 | John |
A4 | Kim |
我认为这是我遗漏的一小部分但是.. 关于如何 filter/sort 它始终使用该技术和其他部门的任何想法?
顺便说一句,我尝试搜索但找不到像我这样的问题..
试试这个:
SELECT E.employeeCode, E.employeeName
FROM Employees E
INNER JOIN (SELECT DISTINCT E1.employeeCode, E1.employeeName
FROM Employees E
WHERE E.Department = 'Technology'
) AS A ON E.employeeCode = A.employeeCode AND E.employeeName = A.employeeName
GROUP BY E.employeeCode, E.employeeName
HAVING COUNT(*) > 1;
如果您想要技术部门和其他部门的员工:
select e.employeeCode, e.employeeName
from employees e
group by e.employeeCode, e.employeeName
having sum(case when e.department = 'Technology' then 1 else 0 end) > 0 and
count(*) > 1;
假设 table 中没有重复项。如果它可以有重复项,则使用 count(distinct department) > 1
而不是 count(*) > 1
.
您可以在具有不同条件的相同 table 上将 EXISTS 与关联子查询结合使用。
SELECT e1.employeeCode, e1.employeeName
FROM Employees e1
WHERE e1.Department = 'Technology'
AND EXISTS (SELECT * FROM Employees e2
WHERE e1.employeeCode = e2.employeeCode
AND e2.Department <> 'Technology')
这适用于您的情况:
SELECT a.employeeCode, a.employeeName
FROM Employees a, Employees b
WHERE
a.Department = 'Technology'
AND
b.Department <> 'Technology'
AND
a.employeeCode = b.employeeCode
AND
a.employeeID <> b.employeeID