查询重复记录

时间:2013-11-08 03:55:02

标签: sql sql-server

我有一张表[员工],我想搜索重复的名称并查看每个部门的电话

Name    Tel Department
Drew    2134    IT
Jason   34532   IT
Drew    35223   Sales
Jason   12313   Sales
Cruto   32432   IT
John    1234    IT
John    4321    Sales
Rex     5643    Sales

如何查询此输出

Name    IT Tel NO   Sales Tel NO
Drew     2134          35223
Jason    34532         12313
John     1234          4321

1 个答案:

答案 0 :(得分:2)

SELECT IT.[Name],IT.[Tel] as IT , Sales.[Tel] as Sales FROM
(SELECT [Name], [Tel]
FROM Table1 WHERE [Department] = 'IT') AS IT
INNER JOIN
(SELECT [Name], [Tel]
FROM Table1 WHERE [Department] = 'Sales') AS Sales
ON IT.[Name] = Sales.[Name]

<强> FIDDLE