Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Combinazione di due colonne sql con dati delimitati mediante confronto in un'unica colonna con dati delimitati

Usando solo espressioni di tabella comuni, possiamo anche ottenere i risultati richiesti, come di seguito:-

Per prima cosa configuriamo i dati

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'  )

Ora, costruiamo le espressioni di tabella comuni:-

;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] 

I risultati saranno i seguenti:-

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