Home > Blockchain >  SQL Server Query (Combining Two rows into one)
SQL Server Query (Combining Two rows into one)

Time:04-12

I have data in following format in SQL Server database.

SearchID Text1 Text
Search1 Black NULL
Search1 NULL Hammer
Search2 Telephone NULL
Search2 NULL Marker

I need data in following format.

SearchID Text1 Text
Search1 Black Hammer
Search2 Telephone Marker

Since text1 and text2 could have any random text, I couldn't use pivot. Any help will be appreciated. Thank you.

CodePudding user response:

You could use max and group by. Something like

SELECT
 searchid, max(text1), max(text)
FROM mytable
GROUP BY searchid

This works with your sample data, where there is one non-null text and one non-null text1 per searchid that you care about.

  • Related