sql >> Base de Datos >  >> RDS >> Sqlserver

Combinación de dos columnas sql con datos delimitados mediante la recopilación en una sola columna con datos delimitados

Usando solo expresiones de tabla comunes, también podemos obtener los resultados requeridos, como se muestra a continuación:-

Primero configuremos los datos

declare @original table(
[value] varchar(250), 
[unit] varchar(250)
)
insert into @original values 
('4 ; 5','mg ; kg    '),
('50','mg        '                ),
('7.5 ; 325','kg ; mg    '        ),
('100 ; 1.5 ; 50 ','mg ; g ; mg'  )

Ahora, construyamos las expresiones de tabla comunes:-

;with cte as (
    select o.[value]+';' [value],o.[unit]+';' [unit],row_number() over (ORDER BY (Select 0)) [row]   from @original o
),cte2 as (
    select *
    ,1 [ValueStart],CHARINDEX(';',[value]) [ValueEnd]
    ,1 [UnitStart],CHARINDEX(';',[unit]) [UnitEnd]
     from cte
),cte3 as (
    select * from cte2
    union all 
    select [value],[unit],[row]
        ,[ValueEnd]+1 [ValueStart],CHARINDEX(';',[value],[ValueEnd]+1) [ValueEnd] 
        ,[UnitEnd]+1 [UnitStart],CHARINDEX(';',[unit],[UnitEnd]+1) [UnitEnd] 
    from cte3 where [UnitEnd]>0
),cte4 as (
    select *,row_number() over (partition by [row] order by [row]) [subRow]
    , rtrim(ltrim(substring([unit],[UnitStart],[UnitEnd]-[UnitStart])))  [subUnit] 
    , rtrim(ltrim(substring([value],[ValueStart],[ValueEnd]-[ValueStart])))  [subValue] 
    from cte3
    where [UnitEnd]>0
),cte5 as (
    select subRow,[row],[subValue],[subUnit],cast([subValue]+' '+[subUnit] as varchar(max)) [ValueUnit] from cte4 where subRow=1
    union all
    select cte4.subRow,cte4.[row],cte4.[subUnit],cte4.[subValue]
        ,cte5.[ValueUnit]+';'+ cte4.[subValue]+' '+cte4.[subUnit] [ValueUnit] 
            from cte4
        inner join cte5 on (cte5.subRow+1)=cte4.subRow and cte5.[row]=cte4.[row]
),cte6 as (
    select *,row_number() over (partition by [row] order by subRow desc) [selected] from cte5
)
select ValueUnit from cte6
where [selected]=1
order by [row] 

Los resultados serán los siguientes:-

ValueUnit
============
4 mg;5 kg 
50 mg  
7.5 kg;325 mg 
100 mg;1.5 g;50 mg