I'm trying to pivot a list of emails onto a single row, but for some reason my primary email address is getting a row of its own, so Im ending up with two rows per contact.
CREATE TABLE #tmp (
ID VARCHAR(20)
, EMAIL VARCHAR(50)
, isPrimary VARCHAR(1)
)
INSERT INTO #tmp (ID, EMAIL, isPrimary) VALUES ('B4100358183Q!ZH.ETES', '[email protected]', '1')
INSERT INTO #tmp (ID, EMAIL, isPrimary) VALUES ('B4100358183Q!ZH.ETES', '[email protected]', '0')
INSERT INTO #tmp (ID, EMAIL, isPrimary) VALUES ('B4100358183Q!ZH.ETES', '[email protected]', '0')
How would I amend the script below to return only one row containing all 3 email addresses rather than two rows?
SELECT ID, [1], [2], [3]
FROM (
Select ID, EMAIL, isPrimary
, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY isPrimary desc) AS rn
from #tmp
) eml
PIVOT (
min(EMAIL)
FOR rn IN ([1],[2],[3])
) P
Any help would be appreciated, thanks.