I have a table called employee with following fields
Id | Name | PrimaryEmail1 | PrimaryEmail2 | PrimaryEmail3 1 | JonSkeet | NULL | NULL | jonskeet@test.com 1 | JonSkeet | NULL | Jon@test.com | jonskeet@test.com 1 | JonSkeet | skeet@test.com | NULL | jonskeet@test.com 2 | George | G1@Test.com | NULL | NULL 2 | George | NULL | G2@test.com | NULL 2 | George | NULL | NULL | G3@test.com
How to write a TSQL query so that i can obtain the following resultset?
Id | Name | PrimaryEmail1 | PrimaryEmail2 | PrimaryEmail3 1 | JonSkeet | skeet@test.com | jon@test.com | jonskeet@test.com 2 | George | G1@test.com | G2@test.com | G3@test.com
MAX will eliminate NULLs
However, your example for JonSkeet shows PrimaryEmail3 is the same. Will you ever expect different values per ID+Name? If so, you can’t combine into a single row unless you choose the MAX one (or MIN even).
This assumes that Id-Name pairs are consistent too