I have a temp table with a number of non distinct customer records, and I want to update the same column in all rows for the same email address, if they have an existing value:
CustomerID | Email | Pref1 | Pref2 | Pref3
-----------------------------------------------------
1234 email1@domain.com 1 0 0
1235 email1@domain.com 1 1 0
1236 email1@domain.com 0 0 1
1237 email2@domain.com 0 0 0
1238 email2@domain.com 1 0 0
Should become:
CustomerID | Email | Pref1 | Pref2 | Pref3
-----------------------------------------------------
1234 email1@domain.com 1 1 1
1235 email1@domain.com 1 1 1
1236 email1@domain.com 1 1 1
1237 email2@domain.com 1 0 0
1238 email2@domain.com 1 0 0
Currently I have a while loop (loop over the rows that have yet to be updated), that contains an inner cursor to loop over the columns I want to update (Pref1,Pref2,Pref3, etc). This works, but takes forever on a larger recordset.
How can I modify the below:
UPDATE #table
SET Pref1 =
(
SELECT MAX(IsNull(Pref1,0))
FROM #table
WHERE Email = 'email1@domain.com'
)
WHERE Email = 'email1@domain.com'
so that instead of passing in every email address, somehow refer to the email address of the record it is updating?
UPDATE #table
SET Pref1 =
(
SELECT MAX(IsNull(Pref1,0))
FROM #table
WHERE Email = #table.email
)
(The above query doesn't work, it simply updates that entire column for all records to 1 if it exists in that column). Should I be using some sort of update join?