如果您想取出重复的行并修改剩余的行以识别它,您可以使用 t
WITH DUPE (MAIL_ADDRESS_1)
AS
(
SELECT MAIL_ADDRESS_1
FROM PERSONAL
GROUP BY MAIL_ADDRESS_1
HAVING COUNT(MAIL_ADDRESS_1) > 1
)
SELECT FIRST_NAME, LAST_NAME , MAIL_ADDRESS_1, PHONE FROM PERSONAL WHERE MAIL_ADDRESS_1 NOT IN(
SELECT MAIL_ADDRESS_1 FROM DUPE)
UNION
SELECT TOP 1 FIRST_NAME, LAST_NAME + '(DUPE MAIL)' AS LAST_NAME, MAIL_ADDRESS_1, PHONE
FROM PERSONAL WHERE MAIL_ADDRESS_1 IN (SELECT MAIL_ADDRESS_1 FROM DUPE)
或简单的选择:
SELECT FIRST_NAME, LAST_NAME , MAIL_ADDRESS_1, PHONE FROM PERSONAL WHERE MAIL_ADDRESS_1 NOT IN(
SELECT MAIL_ADDRESS_1
FROM PERSONAL
GROUP BY MAIL_ADDRESS_1
HAVING COUNT(MAIL_ADDRESS_1) > 1
)
UNION
SELECT TOP 1 FIRST_NAME, LAST_NAME + '(DUPE MAIL)' AS LAST_NAME, MAIL_ADDRESS_1, PHONE
FROM PERSONAL WHERE MAIL_ADDRESS_1 IN (
SELECT MAIL_ADDRESS_1
FROM PERSONAL
GROUP BY MAIL_ADDRESS_1
HAVING COUNT(MAIL_ADDRESS_1) > 1
)
ORDER BY MAIL_ADDRESS_1