SQL Server在第1列中查找重复项,同时在第2列中请求2个特定值

时间:2014-09-14 12:44:25

标签: sql sql-server duplicates relational-division

我有一张这样的表:

Id  first_name  department
1   John        IT
2   George      Support
3   Jack        IT
4   Jack        IT
5   George      Dev
6   Maria       Dev
7   George      IT

我想要得到这个:

Id  first_name  department
5   George      Dev
7   George      IT

我被困了:

SELECT * 
FROM tbl_employees 
WHERE first_name in ( SELECT first_name FROM tbl_employees where (department = 'IT' or department =  'Dev') 
GROUP BY first_name 
HAVING count( first_name ) > 1 )

4 个答案:

答案 0 :(得分:1)

如果您尝试获取原始行,可以执行以下操作:

select e.*
from tbl_employees e
where e.department in ('IT', 'DEV') and
      exists (select 1 from tbl_employees e2 where e2.first_name = e.first_name and e2.department = 'IT') and
      exists (select 1 from tbl_employees e2 where e2.first_name = e.first_name and e2.department = 'DEV');

答案 1 :(得分:1)

SQL Fiddle

MS SQL Server 2008架构设置

查询1

DECLARE @TABLE TABLE(Id INT, first_name VARCHAR(20), department VARCHAR(20))
INSERT INTO @TABLE VALUES
(1   ,'John',   'IT'),
(2   ,'George', 'Support'),
(3   ,'Jack',   'IT'),
(4   ,'Jack',   'IT'),
(5   ,'George', 'Dev'),
(6   ,'Maria',  'Dev'),
(7   ,'George', 'IT'),
(8  ,'Maria',  'Support')

SELECT * FROM @TABLE 
WHERE first_name IN (
                    SELECT first_name FROM @TABLE WHERE department = 'Dev'
                    INTERSECT 
                    SELECT first_name FROM @TABLE WHERE department = 'IT'
                    )
AND department IN ('Dev', 'IT')

<强> Results

| ID | FIRST_NAME | DEPARTMENT |
|----|------------|------------|
|  5 |     George |        Dev |
|  7 |     George |         IT |

答案 2 :(得分:0)

你可以试试这个

    SELECT T.first_name,T.department
    FROM @TABLE T
    INNER JOIN  
   (        SELECT Id, first_name
            FROM @TABLE T2
            WHERE EXISTS ( 
                         SELECT 1 FROM @TABLE T3 
                         WHERE T3.first_name = T2.first_name and T3.department = 'Dev'
                         )
            AND   EXISTS ( SELECT 1 FROM @TABLE T4 
                         WHERE T4.first_name = T2.first_name and T4.department = 'IT'
                         )
      ) ElementsBelongingToBothDevAndIT ON ElementsBelongingToBothDevAndIT.Id = T.Id
    INNER JOIN 
   (
          SELECT first_name
          FROM @TABLE T2
          WHERE EXISTS (
                        SELECT 1 FROM @TABLE T3 
                        WHERE T3.first_name = T2.first_name and T3.department = 'Dev'
                       )
          AND   EXISTS ( 
                        SELECT 1 FROM @TABLE T4 
                        WHERE T4.first_name = T2.first_name and   T4.department = 'IT'
                       ) 
          GROUP BY first_name
           HAVING COUNT(*) > 1
  ) ElementsBelongingToBothDevAndITHavingDuplicates 
    ON ElementsBelongingToBothDevAndITHavingDuplicates.first_name=
    ElementsBelongingToBothDevAndIT.first_name
 WHERE T.department IN ('Dev','IT')
 GROUP BY T.first_name, T.department

答案 3 :(得分:-1)

经过多方努力,我找到了另一个解决我自己问题的方法,这个方案更适合生成 C#中的查询。 此外,我添加了last_name列,因此当我使用intersect时,它会使事情变得复杂。

所以我最终得到了这个

SQL Fiddle

查询1

DECLARE @TABLE TABLE(Id INT, first_name VARCHAR(20), department VARCHAR(20))
INSERT INTO @TABLE VALUES
(1   ,'John',   'IT'),
(2   ,'George', 'Support'),
(3   ,'Jack',   'IT'),
(4   ,'Jack',   'IT'),
(5   ,'George', 'Dev'),
(6   ,'Maria',  'Dev'),
(7   ,'George', 'IT'),
(8  ,'Maria',  'Support')


 SELECT y.Id,y.first_name,y.department
 FROM @table y 
 INNER JOIN (SELECT first_name, COUNT(*) AS CountOf, COUNT(distinct department) as CountDep
             FROM @table
             WHERE department in  ('IT','Dev')
             GROUP BY first_name
             HAVING COUNT(*) > 1 and COUNT(distinct department) > 1
             ) dt ON y.first_name=dt.first_name 
             and department in ('IT','Dev')
    order by first_name

<强> Results

| ID | FIRST_NAME | DEPARTMENT |
|----|------------|------------|
|  5 |     George |        Dev |
|  7 |     George |         IT |

因为我从复选框列表中传递搜索参数,更容易操作部门及其数量。此外,如果我想在一个部门中找到重复项,我只需要在COUNT部门的部分中输入&gt; 0