Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Merging non Null values

Author  Topic 

mike26
Starting Member

5 Posts

Posted - 2012-02-10 : 15:52:09
I have the following table:

ID COL1 COL2 COL3 COL4

1 A NULL 15 NULL
1 A TEST1 NULL NULL
1 A NULL NULL ABC
2 B NULL 10 NULL
2 B TEST2 NULL EFG

I would like to get the following table from the above table

ID COL1 COL2 COL3 COL4
1 A TEST1 15 ABC
2 B TEST2 10 EFG

I have tried self join and checking for null values but so far I haven't been able to get it right. Any suggestions would be appreciated.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-10 : 16:00:25
[code]
SELECT ID,
COL1,
MAX(COL2) AS COL2,
MAX(COL3) AS COL3,
MAX(COL4) AS COL4
FROM Table
GROUP BY ID,COL1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mike26
Starting Member

5 Posts

Posted - 2012-02-10 : 16:02:57
It worked. Thanks a bunch.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-10 : 16:03:41
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -