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

Merge multiple lines into one

I have a large dataset (1.000.000+ lines) from witch I want to merge multible lines based on a key.

To make things clear, I’ve made a minimal viable product:

First some test-data

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

declare @TmpTable table
(
   Pnr varchar(10),
   Status varchar(10),
   Komkod  varchar(10),
   Fornvn varchar(10)
)

insert into @TmpTable values
('01010101', '01', null, null ),
('01010101', null , '0430', null ),
('01010101', null ,  null, 'Test' ),

('02020202', '10', null, null ),
('02020202', null, '3004', null ),
('02020202', null ,  null, 'Test' )

Then the output:

enter image description here

I want to merge the 6 lines into 2.

I’ve tried diffrent things (Windowed function, Inner join) but never found a good solution.

So now I need some good ideas. Perforce is king.

>Solution :

You can simply aggregate which automatically ignores nulls:

select pnr,
  Max(status) Status,
  Max(Komkod) Komkod,
  Max(Fornvn) Fornvn
from @TmpTable
group by Pnr;
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