Merge Rows having NULL values in SQL Server

Last Reply one year ago By dharmendr

Posted one year ago
  Term-I Term-II orderno
Art Education B   14
Computer Application   E 4
Computer Application A   4
Health & Physical Education C   15
Work Education D   16

i want to merge computer application in single row. how can i do this?

Posted one year ago

Hi iammann,

Refer the below test query.

SQL

DECLARE @Test AS TABLE(Name VARCHAR(50),[Term-I] CHAR(1),[Term-II] CHAR(1),orderno INT)

INSERT INTO @Test VALUES('Art Education','B',NULL,14)
INSERT INTO @Test VALUES('Computer Application',NULL,'E',4)
INSERT INTO @Test VALUES('Computer Application','A',NULL,4)
INSERT INTO @Test VALUES('Health & Physical Education','C',NULL,15)
INSERT INTO @Test VALUES('Work Education','D',NULL,16)

SELECT Name,ISNULL(MAX([Term-I]),'')[Term-I],ISNULL(MAX([Term-II]),'')[Term-II],orderno 
FROM @Test
GROUP BY Name,orderno
ORDER BY Name

Output

Name Term-I Term-II orderno
Art Education B   14
Computer Application A E 4
Health & Physical Education C   15
Work Education D   16