sql >> Databasteknik >  >> RDS >> Sqlserver

Kombinera två sql-kolumner med avgränsade data genom att sortera till en enda kolumn med avgränsade data

Genom att bara använda vanliga tabelluttryck kan vi också nå de önskade resultaten, enligt nedan:-

Låt oss först ställa in data

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

Låt oss nu bygga de vanliga tabelluttrycken:-

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

Resultaten blir enligt nedan:-

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


  1. Hur lägger man till en JDBC-drivrutin till en Jenkins pipeline?

  2. Skanningar av allokeringsorder

  3. Välj värden som börjar med ett tal

  4. mysql-valfråga i en array