如何通过firstname和lastname显示重复的行grupe但是在不同的行中?
我有下一个代码:
SELECT LASTname, FIRSTname, COUNT(*) AS dupes
FROM TCPD_PERSONEL
GROUP BY FIRSTNAME, LASTNAME,
HAVING (COUNT(*) > 1);
当我运行时出现:
lastname firstname dupes
Ion Alin 2
但我希望看起来像这样:
lastname firstname email
Ion Alin alin@yahoo
Ion Alin ion@yahoo
答案 0 :(得分:6)
试试这个
SELECT t1.LASTname,t1.FIRSTname,t1.email from TCPD_PERSONEL t1 inner join
(
SELECT LASTname, FIRSTname, COUNT(*) AS dupes
FROM TCPD_PERSONEL
GROUP BY FIRSTNAME, LASTNAME,
HAVING (COUNT(*) > 1)
) t2 on t1.LASTname=t2.LASTname and t1.FIRSTname=t2.FIRSTname;
答案 1 :(得分:2)
使用分析函数:
SELECT p.*
FROM (SELECT p.*, COUNT(*) OVER (PARTITION BY FIRSTNAME, LASTNAME) as cnt
FROM TCPD_PERSONEL p
) p
WHERE cnt >= 2;
编辑:
如果行没有完全重复(即电子邮件不同),那么以下可能是最有效的方法:
select p.*
from TCPD_PERSONEL p
where exists (select 1
from TCPD_PERSONEL p2
where p2.firstname = p.firstname and p2.lastname = p.lastname and
p2.email <> p.email
);
为了提高性能,您需要TCPD_PERSONEL(lastname, firstname, email)
上的索引。
答案 2 :(得分:2)
您需要再次使用同一个表加入该查询:
SELECT * FROM TCPD_PERSONEL
INNER JOIN
(
SELECT LASTname, FIRSTname, COUNT(*) AS dupes
FROM TCPD_PERSONEL
GROUP BY FIRSTNAME, LASTNAME
HAVING (COUNT(*) > 1)
) x
ON x.LASTname = TCPD_PERSONEL.LASTname AND
x.FIRSTname = TCPD_PERSONEL.FIRSTname;
答案 3 :(得分:0)
试试这个:
SELECT * FROM tblInfo INNER JOIN (
SELECT firstname, lastname, COUNT(*) AS temp
FROM tblInfo
GROUP BY lastname, firstname
HAVING (COUNT(*) > 1)
) x
ON x. firstname = tblInfo. firstname AND
x. lastname = tblInfo. lastname;
将'tblInfo'替换为'TCPD_PERSONEL'
答案 4 :(得分:-1)
查看您的代码,我认为Distinct是您的最佳选择。
SELECT DISTINCT LASTname, FIRSTname,email FROM TCPD_PERSONEL