For a database with multiple rows per e-mail address, I want to group by each email address, taking the "most recent" information for each e-mail address.
Email Col1 Col2 Col3 Col4 CustomerID
======= ==== ==== ==== ==== ==========
a@a.com a a a null 1
a@a.com null b b null 2
a@a.com null null c null 3
I want to take the non-null value with the highest CustomerID
. For above, I'd expect:
Email Col1 Col2 Col3 Col4
======= ==== ==== ==== ====
a@a.com a b c null
I can do a GROUP BY
, taking the MAX
for each column, but it's simply the highest value alphabetically, and doesn't take CustomerID
into consideration.
SELECT EmailAddress, MAX(FirstName), MAX(LastName), MAX(Gender), MAX(Birthday), MAX(Country)
FROM CustomerInfo
GROUP BY EmailAddress
Additionally, this is being programmed in Exact Target, meaning some SQL keywords are unsupported, most notably variables, temp tables, and cursors are not supported.
Given these restrictions, is it possible to get the desired results?