在此SQL查询之后:
SELECT DISTINCT *
FROM ['Duplicate Data$']
WHERE Name in
(
SELECT Name
FROM ['Duplicate Data$']
GROUP BY Name
HAVING COUNT(*)>1
)
ORDER BY Name
我的表格如下:
ID | Name
20215 | A Abbott
22015 | A Abbott
11825 | A Albert
20745 | A Albert
14109 | A Allan
19696 | A Allan
13133 | A Allan
17311 | A Allen
我需要查询此表,以便返回结果,如:
ID | Name
20215 | A Abbott
11825 | A Albert
13133 | A Allan
答案 0 :(得分:4)
SELECT MIN(ID),Name
FROM ['Duplicate Data$']
GROUP BY Name
HAVING COUNT(*)>1
ORDER BY Name
答案 1 :(得分:1)
试试这个
SELECT *
FROM ['Duplicate Data$'] A
WHERE ID = (SELECT MIN(ID)
FROM ['Duplicate Data$'] X
WHERE A.NAME = X.NAME)
答案 2 :(得分:0)
在子查询或CTE中使用由row_number()
分区的name
函数:
create table #t
(ID int, Name varchar(10));
insert #t values
( 20215, 'A Abbott'),
( 22015, 'A Abbott'),
( 11825, 'A Albert'),
( 20745, 'A Albert'),
( 14109, 'A Allan'),
( 19696, 'A Allan'),
( 13133, 'A Allan'),
( 17311, 'A Allen');
select id, name
from (
select *, row_number() over(partition by name order by id) rn from #t
) x
where rn = 1;
(最后一行中的“A Allen”是拼写错误,对吗?)