sql >> Database >  >> RDS >> Sqlserver

Combineren van twee sql-kolommen met gescheiden gegevens door ze te verzamelen in een enkele kolom met gescheiden gegevens

Door alleen algemene tabeluitdrukkingen te gebruiken, kunnen we ook de vereiste resultaten krijgen, zoals hieronder:-

Laten we eerst de gegevens instellen

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

Laten we nu de algemene tabeluitdrukkingen bouwen:-

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

De resultaten zijn als volgt:-

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


  1. $resultaat =mysql_query()

  2. Waarden selecteren die aan verschillende voorwaarden voldoen op verschillende rijen?

  3. Hoe MySQL-client automatisch opnieuw verbinden met MySQLdb inschakelen?

  4. Buitenlandse sleutel toevoegen aan bestaande tabel