SQL Server 2008, I have inherited a table like this (74k rows):
create table #mess (keycol char(36) , name1 varchar(254) , name2 varchar(254), valuex varchar(254) )
insert into #mess values ('971EC307-8514-450D-AE3A-4E25EA3F3A10' , 'a' , '' , '' )
insert into #mess values ('971EC307-8514-450D-AE3A-4E25EA3F3A10' , '' , 'a' , 'value-a' )
insert into #mess values ('04FD0C0B-FC90-405A-BFD6-C3AF2516E51F' , 'b' , '' , 'value-b' )
insert into #mess values ('578F2893-15E6-4877-9FE6-AC2F4F351143' , 'c' , '' , 'value-c' )
insert into #mess values ('04FD0C0B-FC90-405A-BFD6-C3AF2516E51F' , '' , 'b' , '' )
insert into #mess values ('FAFCBDFE-D49E-4566-882D-0B6628DA59CC' , '' , 'd' , 'value-d' )
Which makes this result set:
keycol name1 name2 valuex
------------------------------------ ------- -------- ------------
971EC307-8514-450D-AE3A-4E25EA3F3A10 a
971EC307-8514-450D-AE3A-4E25EA3F3A10 a value-a
04FD0C0B-FC90-405A-BFD6-C3AF2516E51F b value-b
578F2893-15E6-4877-9FE6-AC2F4F351143 c value-c
04FD0C0B-FC90-405A-BFD6-C3AF2516E51F b
FAFCBDFE-D49E-4566-882D-0B6628DA59CC d value-d
I need to make it like this (de-duplicating and collapsing the data, based on when keycol
matches between two rows, and using name1
if name2
is empty and vice versa but always using name1
and always using non-empty valuex
column). All ideas appreciated.
Thanks.
keycol name1 valuex
------------------------------------ ------- ------------
971EC307-8514-450D-AE3A-4E25EA3F3A10 a value-a
04FD0C0B-FC90-405A-BFD6-C3AF2516E51F b value-b
578F2893-15E6-4877-9FE6-AC2F4F351143 c value-c
FAFCBDFE-D49E-4566-882D-0B6628DA59CC d value-d