我想提取" KEY"的值。和" VALUE"并使用带有EXTRACT的XPath连接这些结果。
示例XML如下。
<PivotSet>
<item>
<column name = "KEY">RET_1</column>
<column name = "VALUE">A</column>
</item>
<item>
<column name = "KEY">RET_2</column>
<column name = "VALUE">R</column>
</item>
<item>
<column name = "KEY">RET_3</column>
<column name = "VALUE">P</column>
</item>
</PivotSet>
我需要在SQL中返回类似
的内容RET_1@A;RET_2@R;RET_3@P
分隔符&#39; @&#39;分隔键/值和&#39;:&#39;分开项目。我想用EXTRACT和XPath来做。
答案 0 :(得分:0)
您可以将ListAgg与XmlTable一起使用,请参阅下文:
select LISTAGG(keyItem || '@' || keyValue, ';') WITHIN GROUP (ORDER BY keyItem) AS Concated
from XmlTable( '/PivotSet/item' PASSING XmlType('<PivotSet>
<item>
<column name = "KEY">RET_1</column>
<column name = "VALUE">A</column>
</item>
<item>
<column name = "KEY">RET_2</column>
<column name = "VALUE">R</column>
</item>
<item>
<column name = "KEY">RET_3</column>
<column name = "VALUE">P</column>
</item>
</PivotSet>') COLUMNS keyItem PATH '*:column[@name = "KEY"]',
keyValue PATH '*:column[@name = "VALUE"]');
您可以使用ExtractValue,但是您需要知道XML中有多少//item
个元素,然后在提取调用的路径部分中指出,但这有点混乱IMO:
SELECT ExtractValue(xmlOut, '//item[1]/column[@name="KEY"]') || '@' || ExtractValue(xmlOut, '//item[1]/column[@name="VALUE"]') || ';' ||
ExtractValue(xmlOut, '//item[2]/column[@name="KEY"]') || '@' || ExtractValue(xmlOut, '//item[2]/column[@name="VALUE"]') || ';' ||
ExtractValue(xmlOut, '//item[3]/column[@name="KEY"]') || '@' || ExtractValue(xmlOut, '//item[3]/column[@name="VALUE"]') As Concatted
FROM
(
SELECT XmlType('<PivotSet>
<item>
<column name = "KEY">RET_1</column>
<column name = "VALUE">A</column>
</item>
<item>
<column name = "KEY">RET_2</column>
<column name = "VALUE">R</column>
</item>
<item>
<column name = "KEY">RET_3</column>
<column name = "VALUE">P</column>
</item>
</PivotSet>') as xmlOut FROM dual
);