- 単にyour_table_nameでtab
を置き換える:
:それは私に結果を与える
WITH
tab2 AS (
SELECT t.field,
CASE WHEN INSTR(t.field, ',', 1, 1) > 0 AND regexp_count(t.field,',') >= 1 THEN INSTR(t.field, ',', 1, 1) ELSE NULL END AS pos1,
CASE WHEN INSTR(t.field, ',', 1, 2) > 0 AND regexp_count(t.field,',') >= 2 THEN INSTR(t.field, ',', 1, 2) ELSE NULL END AS pos2,
CASE WHEN INSTR(t.field, ',', 1, 3) > 0 AND regexp_count(t.field,',') >= 3 THEN INSTR(t.field, ',', 1, 3) ELSE NULL END AS pos3,
CASE WHEN INSTR(t.field, ',', 1, 4) > 0 AND regexp_count(t.field,',') >= 4 THEN INSTR(t.field, ',', 1, 4) ELSE NULL END AS pos4,
CASE WHEN INSTR(t.field, ',', 1, 5) > 0 AND regexp_count(t.field,',') >= 5 THEN INSTR(t.field, ',', 1, 5) ELSE NULL END AS pos5,
CASE WHEN INSTR(t.field, ',', 1, 6) > 0 AND regexp_count(t.field,',') >= 6 THEN INSTR(t.field, ',', 1, 6) ELSE NULL END AS pos6
FROM tab t
),
tab3 AS (
SELECT SUBSTR(tt.field,1,tt.pos1-1) AS col1,
SUBSTR(tt.field,tt.pos1+1, CASE WHEN tt.pos2 IS NULL THEN LENGTH(tt.field) - tt.pos1 ELSE tt.pos2 - tt.pos1 - 1 END) AS col2,
SUBSTR(tt.field,tt.pos2+1, CASE WHEN tt.pos3 IS NULL THEN LENGTH(tt.field) - tt.pos2 ELSE tt.pos3 - tt.pos2 - 1 END) AS col3,
SUBSTR(tt.field,tt.pos3+1, CASE WHEN tt.pos4 IS NULL THEN LENGTH(tt.field) - tt.pos3 ELSE tt.pos4 - tt.pos3 - 1 END) AS col4,
SUBSTR(tt.field,tt.pos4+1, CASE WHEN tt.pos5 IS NULL THEN LENGTH(tt.field) - tt.pos4 ELSE tt.pos5 - tt.pos4 - 1 END) AS col5,
SUBSTR(tt.field,tt.pos5+1, CASE WHEN tt.pos6 IS NULL THEN LENGTH(tt.field) - tt.pos5 ELSE tt.pos6 - tt.pos5 - 1 END) AS col6
,ROWNUM AS r
FROM tab2 tt
),
tab4 AS (
SELECT ttt.col1 AS col FROM tab3 ttt WHERE r = 1
UNION ALL SELECT ttt.col2 FROM tab3 ttt WHERE r = 1
UNION ALL SELECT ttt.col3 FROM tab3 ttt WHERE r = 1
UNION ALL SELECT ttt.col4 FROM tab3 ttt WHERE r = 1
UNION ALL SELECT ttt.col5 FROM tab3 ttt WHERE r = 1
UNION ALL SELECT ttt.col6 FROM tab3 ttt WHERE r = 1
UNION ALL
SELECT ttt.col1 FROM tab3 ttt WHERE r = 2
UNION ALL SELECT ttt.col2 FROM tab3 ttt WHERE r = 2
UNION ALL SELECT ttt.col3 FROM tab3 ttt WHERE r = 2
UNION ALL SELECT ttt.col4 FROM tab3 ttt WHERE r = 2
UNION ALL SELECT ttt.col5 FROM tab3 ttt WHERE r = 2
UNION ALL SELECT ttt.col6 FROM tab3 ttt WHERE r = 2
UNION ALL
SELECT ttt.col1 FROM tab3 ttt WHERE r = 3
UNION ALL SELECT ttt.col2 FROM tab3 ttt WHERE r = 3
UNION ALL SELECT ttt.col3 FROM tab3 ttt WHERE r = 3
UNION ALL SELECT ttt.col4 FROM tab3 ttt WHERE r = 3
UNION ALL SELECT ttt.col5 FROM tab3 ttt WHERE r = 3
UNION ALL SELECT ttt.col6 FROM tab3 ttt WHERE r = 3
UNION ALL
SELECT ttt.col1 FROM tab3 ttt WHERE r = 4
UNION ALL SELECT ttt.col2 FROM tab3 ttt WHERE r = 4
UNION ALL SELECT ttt.col3 FROM tab3 ttt WHERE r = 4
UNION ALL SELECT ttt.col4 FROM tab3 ttt WHERE r = 4
UNION ALL SELECT ttt.col5 FROM tab3 ttt WHERE r = 4
UNION ALL SELECT ttt.col6 FROM tab3 ttt WHERE r = 4
UNION ALL
SELECT ttt.col1 FROM tab3 ttt WHERE r = 5
UNION ALL SELECT ttt.col2 FROM tab3 ttt WHERE r = 5
UNION ALL SELECT ttt.col3 FROM tab3 ttt WHERE r = 5
UNION ALL SELECT ttt.col4 FROM tab3 ttt WHERE r = 5
UNION ALL SELECT ttt.col5 FROM tab3 ttt WHERE r = 5
UNION ALL SELECT ttt.col6 FROM tab3 ttt WHERE r = 5
)
SELECT col
FROM tab4
WHERE col IS NOT NULL
1 Hello
2 world
3 !
4 Haloa
5 !
6 Have
7 a
8 nice
9 day
10 !
DBMSによって異なります。分割関数をいくつか実装しています(多くの言語によく似ています)。各テーブルレコード(フィールド)に '、 'をセパレータとして使用する必要があります。 DBMSで使用できない場合は、単純な配列/カーソル/結果セットを返す単純な関数を書くことができます。 – FDavidov
使用しているdbmsにタグを付けます。 – jarlh
最初にSQLテーブルにコンマ区切りの値を格納しないと、多くの頭痛を防ぐことができます。あなたはデータベースを支配しているようです。完全に回避できる問題の回避策を作成して時間を浪費するのではなく、適切に設計してください。 – Tomalak