Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine multiple rows with different column values into a single one

I’m trying to create a single row starting from multiple ones and combining them based on different column values; here is the result i reached based on the following query:

select distinct ID, case info when 'name' then value end as 'NAME', case info when 'id' then value end as 'serial'
FROM TABLENAME t 
WHERE info = 'name' or info = 'id'

enter image description here

Howerver the expected result should be something along the lines of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I tried with group by clauses but that doesn’t seem to work.
The RDBMS is Microsoft SQL Server.
Thanks

>Solution :

SELECT X.ID,MAX(X.NAME)NAME,MAX(X.SERIAL)AS SERIAL   FROM
(
  SELECT 100 AS ID, NULL AS NAME, '24B6-97F3'AS SERIAL UNION ALL
  SELECT 100,'A',NULL UNION ALL
  SELECT 200,NULL,'8113-B600'UNION ALL
  SELECT 200,'B',NULL
)X
GROUP BY X.ID

For me GROUP BY works

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading