31

假设我有以下 sql 表

    objid  firstname lastname active
     1       test      test     0
     2       test      test     1
     3       test1     test1    1
     4       test2     test2    0
     5       test2     test2    0
     6       test3     test3    1

现在,我感兴趣的结果如下:

     objid  firstname lastname active
     1       test      test     0
     2       test      test     1
     4       test2     test2    0
     5       test2     test2    0

我怎样才能做到这一点?我尝试了以下查询,

select firstname,lastname from table
group by firstname,lastname
having count(*) > 1

但是这个查询给出的结果如下

    firstname  lastname
     test        test
     test2       test2
4

11 回答 11

58

您已找到重复的记录,但您有兴趣获取附加到它们的所有信息。您需要将join副本复制到主表才能获取该信息。

select *
  from my_table a
  join ( select firstname, lastname 
           from my_table 
          group by firstname, lastname 
         having count(*) > 1 ) b
    on a.firstname = b.firstname
   and a.lastname = b.lastname

这与 an 相同inner join,意味着对于子查询中的每条记录,找到重复记录的您从主表中找到具有相同 firstseen 和 lastseen 组合的所有内容。

您也可以使用 in 执行此操作,但您应该测试差异

select *
  from my_table a
 where ( firstname, lastname ) in   
       ( select firstname, lastname 
           from my_table 
          group by firstname, lastname 
         having count(*) > 1 )

延伸阅读:

于 2012-04-25T21:34:22.890 回答
7
SELECT DISTINCT t1.*
FROM myTable AS t1
INNER JOIN myTable AS t2
  ON t1.firstname = t2.firstname
  AND t1.lastname = t2.lastname
  AND t1.objid <> t2.objid

这将输出具有重复项的每一行,基于firstnamelastname

于 2012-04-25T21:36:00.840 回答
6

这是做本的第一个答案的更清晰的方法:

WITH duplicates AS (
   select    firstname, lastname
   from      my_table
   group by  firstname, lastname
   having    count(*) > 1
)
SELECT    a.*
FROM      my_table   a
JOIN      duplicates b ON (a.firstname = b.firstname and a.lastname = b.lastname)
于 2012-04-25T21:48:01.420 回答
6
SELECT user_name,email_ID 
FROM User_Master WHERE 
email_ID 
in (SELECT email_ID 
FROM User_Master GROUP BY 
email_ID HAVING COUNT(*)>1) 

在此处输入图像描述

于 2018-03-28T11:37:19.623 回答
1

不错的选择从表中获取所有重复值

 select * from Employee where Name in (select Name from Employee group by Name having COUNT(*)>1)
于 2014-09-26T17:44:33.987 回答
1

这是最简单的方法:

SELECT * FROM yourtable a WHERE EXISTS (SELECT * FROM yourtable b WHERE a.firstname = b.firstname AND a.secondname = b.secondname AND a.objid <> b.objid)
于 2016-06-20T09:05:44.280 回答
1

如果要打印表中的所有重复 ID:

select * from table where id in (select id from table group By id having count(id)>1)
于 2016-09-17T04:04:41.147 回答
1

我很惊讶使用Window function没有答案。我刚刚遇到了这个用例,这对我有帮助。

select t.objid, t.firstname, t.lastname, t.active
from
(
select t.*, count(*) over (partition by firstname, lastname) as cnt
from my_table t
) t
where t.cnt > 1;

小提琴 - https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=c0cc3b679df63c4d7d632cbb83a9ef13


格式如下

select
    tbl.relevantColumns
from
(
    select t.*, count(*) over (partition by key_columns) as cnt
    from desiredTable t
) as tbl
where tbl.cnt > 1;

count > 1此格式从用于key_columns标识重复行的表中选择您需要的任何列(有时是所有列) 。key_columns可以是任意数量的列。

于 2019-05-03T18:02:17.577 回答
0

这个答案可能不是很好,但我认为它很容易理解。

SELECT * FROM table1 WHERE (firstname, lastname) IN ( SELECT firstname, lastname FROM table1 GROUP BY firstname, lastname having count() > 1);
于 2014-03-27T05:56:02.973 回答
0

此查询返回重复项

SELECT * FROM (
  SELECT  a.* 
    FROM table a 
    WHERE (`firstname`,`lastname`) IN (
        SELECT `firstname`,`lastname` FROM table 
        GROUP BY `firstname`,`lastname` HAVING COUNT(*)>1       
        )  
    )z WHERE z.`objid` NOT IN (
        SELECT MIN(`objid`) FROM table 
        GROUP BY `firstname`,`lastname` HAVING COUNT(*)>1
        )                                         
于 2014-05-03T07:45:51.783 回答
0

请试试

WITH cteTemp AS (
  SELECT EmployeeID, JoinDT,
     row_number() OVER(PARTITION BY EmployeeID, JoinDT ORDER BY EmployeeID) AS [RowFound]
  FROM dbo.Employee 
)
SELECT * FROM cteTemp WHERE [RowFound] > 1 ORDER BY JoinDT
于 2021-04-07T15:11:01.800 回答