sql >> Database teknologi >  >> RDS >> Oracle

Konverter kommaseparerede værdier til rækker i Oracle

Det ville være meget nemmere at normalisere tabel end at finde komplicerede metoder til at konvertere csv til rækker.

I dette tilfælde kan du bruge dette:

with t(class_id, student, marks) as (
    select 1, 'tom,jam,tim', '55,65,75' from dual union all
    select 2, 'rim,gum,ram', '33,66,77' from dual
)
select
    t.class_id,
    regexp_substr(student,'[^,]+',1,x.column_value) student,
    regexp_substr(marks,'[^,]+',1,y.column_value) marks
from t, table(
    cast(
        multiset(
            select level
            from dual
            connect by level <= regexp_count(t.student,',') + 1
        ) as sys.odcinumberlist
    )
)x,table(
    cast(
        multiset(
            select level
            from dual
            connect by level <= regexp_count(t.marks,',') + 1
        ) as sys.odcinumberlist
    )
)y
 

Producerer:

+----------+------------+-------+ | CLASS_ID | STUDENT | MARKS | +----------+------------+-------+ | 1 | tom | 55 | +----------+------------+-------+ | 1 | tom | 65 | +----------+------------+-------+ | 1 | tom | 75 | +----------+------------+-------+ | 1 | jam | 55 | +----------+------------+-------+ | 1 | jam | 65 | +----------+------------+-------+ | 1 | jam | 75 | +----------+------------+-------+ | 1 | tim | 55 | +----------+------------+-------+ | 1 | tim | 65 | +----------+------------+-------+ | 1 | tim | 75 | +----------+------------+-------+ | 2 | rim | 33 | +----------+------------+-------+ | 2 | rim | 66 | +----------+------------+-------+ | 2 | rim | 77 | +----------+------------+-------+ | 2 | gum | 33 | +----------+------------+-------+ | 2 | gum | 66 | +----------+------------+-------+ | 2 | gum | 77 | +----------+------------+-------+ | 2 | ram | 33 | +----------+------------+-------+ | 2 | ram | 66 | +----------+------------+-------+ | 2 | ram | 77 | +----------+------------+-------+

  1. Opdel flere SQL-sætninger i individuelle SQL-sætninger

  2. Hvordan analyserer jeg Json-data fra openlibrary api? (korrekt)

  3. Gruppere efter måned eller 0, hvis ikke resultater?

  4. Find manglende data sendt til SQL in-clause