Skip to content
Advertisement

How to fold table rows with duplicated ID’s in T-SQL

I’ve googled for a while but haven’t found an aswer so I request your help.

I have a subquery which returns me these values:

id | item1 | item2 | item3
---|-------|-------|------
1  | 123   | Null  | Null
1  | Null  | 213   | Null
1  | Null  | Null  | 321

And I need to make it look like this:

id | item1 | item2 | item3
---|-------|-------|------
1  | 123   | 213   | 321

Advertisement

Answer

SELECT id,max(item1) AS item1,max(item2) AS item2,max(item3) AS item3
FROM tab
GROUP BY id
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement