db2 a recursive fields separated in separator

  Many business table to store items of information are separated by commas business data, statistics, when transcoding is very inconvenient to use with X ( '', '' ) convenient as values ( '', '' ) results recursive. db2 a plurality of data fields are separated by a comma-separated list of dictionary data code, it is now sql by converting the field directly corresponding to the code name dictionary, with reference to the following figure:

FIG 1 FIG converted into two

 Figure I

 

 Figure II

 

 

WITH n
    (
        str,
        ori,
        pos,
        USER
    )
    AS 
    (
    VALUES
        (
            concat('1,2',','),
            1,
            posstr(concat('1,2' ,','), ','),
            '402882775f0e5084015f10cdaa00006a'
        )
    UNION ALL     SELECT str,
                    pos+1,
                    locate(',', str, pos+1),
                    USER 
                FROM n 
                WHERE locate(',', str, pos+1)>0 
                )
SELECT LISTAGG(s.CODE_NAME,',') WITHIN GROUP(
ORDER BY n.USER) 
FROM n,
    SYS_CODE_LIST s 
WHERE s.CODE_ID = substr(str,ori,
    CASE pos-ori 
        WHEN -1 
        THEN 32 
        ELSE pos - ori 
    END)

 

Guess you like

Origin www.cnblogs.com/anyongkang/p/11317071.html
db2
db2