重複した行を取り出し、残りの行を変更して識別したい場合は、 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