我对xmltable
进行了查询:
WITH sample_data AS(
SELECT 10001 ID, '"A","B","C"' str, '"val_a","val_b","val_e"' numf FROM dual UNION ALL
SELECT 10002 ID, '"D","E","F"' str, '"val_c","val_a","val_z"' numf FROM dual UNION ALL
SELECT 10003 ID, '"C","E","G"' str, '"val_q","val_f","val_p"' numf FROM dual
)
SELECT id,
trim(n.getstringval()) str
FROM sample_data,
xmltable(str) n
结果我得到:
--id--|--str---
10001 | A
10001 | B
10001 | C
10002 | D
10002 | E
10002 | F
10003 | C
10003 | E
10003 | G
现在我需要得到这个结果:
--id--|--str--|--numf--
10001 | A | val_a
10001 | B | val_b
10001 | C | val_e
10002 | D | val_c
10002 | E | val_a
10002 | F | val_z
10003 | C | val_q
10003 | E | val_f
10003 | G | val_p
如何更新查询以获得此结果?
答案 0 :(得分:2)
您可以使用:
WITH sample_data AS(
SELECT 10001 ID, '"A","B","C"' str, '2,3,4' numf FROM dual UNION ALL
SELECT 10002 ID, '"D","E","F"' str, '5,6,7' numf FROM dual UNION ALL
SELECT 10003 ID, '"C","E","G"' str, '8,9,10'numf FROM dual
), cte2 AS (
SELECT id,
trim(n.getstringval()) str,
NULL AS numf,
rownum AS r
FROM sample_data, xmltable(str) n
UNION ALL
SELECT id,
NULL,
trim(n.getstringval()) numf,
rownum
FROM sample_data, xmltable(numf) n
)
SELECT MIN(ID) AS ID, MIN(str) AS str, MIN(numf) AS numf
FROM cte2
GROUP BY r
ORDER BY ID, str;
输出:
┌───────┬─────┬──────┐
│ ID │ STR │ NUMF │
├───────┼─────┼──────┤
│ 10001 │ A │ 2 │
│ 10001 │ B │ 3 │
│ 10001 │ C │ 4 │
│ 10002 │ D │ 5 │
│ 10002 │ E │ 6 │
│ 10002 │ F │ 7 │
│ 10003 │ C │ 8 │
│ 10003 │ E │ 9 │
│ 10003 │ G │ 10 │
└───────┴─────┴──────┘