假设我有以下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
答案 0 :(得分:36)
您已找到重复的记录,但您有兴趣获取所有相关信息。您需要将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
这与inner join
相同,意味着对于子查询中的每条记录,找到了重复记录,您可以在主表中找到具有相同firstseen和lastseen组合的所有内容。
您也可以使用though you should test the difference:
执行此操作select *
from my_table a
where ( firstname, lastname ) in
( select firstname, lastname
from my_table
group by firstname, lastname
having count(*) > 1 )
答案 1 :(得分:6)
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
这将根据firstname
和lastname
输出具有重复的每一行。
答案 2 :(得分:4)
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)
答案 3 :(得分:3)
这是Ben的第一个答案的一个更清晰的方式:
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)
答案 4 :(得分:1)
nice选项从表中获取所有重复值
select * from Employee where Name in (select Name from Employee group by Name having COUNT(*)>1)
答案 5 :(得分: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)
答案 6 :(得分:1)
如果要打印表格中的所有重复ID:
select * from table where id in (select id from table group By id having count(id)>1)
答案 7 :(得分:0)
这个答案可能不是很好,但我认为这很容易理解。
SELECT * FROM table1 WHERE (firstname, lastname) IN ( SELECT firstname, lastname FROM table1 GROUP BY firstname, lastname having count() > 1);
答案 8 :(得分:0)
此查询返回dupliacates
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
)
答案 9 :(得分:0)
对于使用 Window函数没有答案,我感到很惊讶。我刚遇到这个用例,这对我有帮助。
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
的{{1}}用于标识重复的行。 key_columns
可以是任意数量的列。
答案 10 :(得分: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