Split values in parts with sqlite

前端 未结 2 1426
南笙
南笙 2021-01-13 03:09

I\'m struggling to convert

a | a1,a2,a3
b | b1,b3
c | c2,c1

to:



        
2条回答
  •  再見小時候
    2021-01-13 03:55

    Check my answer in How to split comma-separated value in SQLite?. This will give you the transformation in a single query rather than having to apply to each row.

    -- using your data table assuming that b3 is suppose to be b2
    
    WITH split(one, many, str) AS (
        SELECT one, '', many||',' FROM data
        UNION ALL SELECT one,
        substr(str, 0, instr(str, ',')),
        substr(str, instr(str, ',')+1)
        FROM split WHERE str !=''
    ) SELECT one, many FROM split WHERE many!='' ORDER BY one;
    
    a|a1
    a|a2
    a|a3
    b|b1
    b|b2
    c|c2
    c|c1
    

提交回复
热议问题