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

PLSQL Merge multiple rows into one column

This is my table data.

enter image description here

I need to select that views as a new column.(if available only) this is the result i need

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 this way. but not getting correct results

SELECT        dbo.tables.DocNo, dbo.tables.Types, tables_1.Types AS viewstatus
FROM            dbo.tables INNER JOIN
                         dbo.tables AS tables_1 ON dbo.tables.DocNo = tables_1.DocNo
WHERE        (dbo.tables.Types = N'Original')
GROUP BY dbo.tables.DocNo, dbo.tables.Types, tables_1.Types
HAVING        (tables_1.Types = N'Views')

>Solution :

A solution with ´group by` without a join:

select min(id), 
min(case when Types = 'Original' then Types end), 
DocNo,
max(Name),
min(case when Types = 'Views' then Types end)
from table_name
group by DocNo;

Fiddle

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