我需要一个xml来创建视图。 具有带有xml数据的表: tbl1(xml数据)
我想对此进行查看,以便数据采用关系形式。 xml的结构如下:
<persons>
<person>
<name>BLA1</name>
<parameters>
<param>
<key>KKK1</key>
<value>VVV1</value>
</param>
<param>
<key>KKK2</key>
<value>VVV2</value>
</param>
</parameters>
</person>
<person>
<name>BLA2</name>
<parameters>
<param>
<key>KKK21</key>
<value>VVV21</value>
</param>
<param>
<key>KKK22</key>
<value>VVV22</value>
</param>
<param>
<key>KKK23</key>
<value>VVV23</value>
</param>
</parameters>
</person>
</persons>
,新表中的所需模型为: (NAME,PARAMS) 其中PARAMS的格式应为key = value; key = value;
即示例行为: 'BLA1''KKK1 = VVV1; KKK2 = VVV2;'
答案 0 :(得分:1)
select name, listagg(key||'='||value, ';') params
from
xmltable( '$doc/persons/person/parameters/param' passing
XMLPARSE(DOCUMENT '
<persons>
<person>
<name>BLA1</name>
<parameters>
<param>
<key>KKK1</key>
<value>VVV1</value>
</param>
<param>
<key>KKK2</key>
<value>VVV2</value>
</param>
</parameters>
</person>
<person>
<name>BLA2</name>
<parameters>
<param>
<key>KKK21</key>
<value>VVV21</value>
</param>
<param>
<key>KKK22</key>
<value>VVV22</value>
</param>
<param>
<key>KKK23</key>
<value>VVV23</value>
</param>
</parameters>
</person>
</persons>
') as "doc"
COLUMNS "NAME" VARCHAR(64) PATH '../../name'
, "KEY" VARCHAR(64) PATH './key'
, "VALUE" VARCHAR(64) PATH './value'
) as x
group by name;
答案 1 :(得分:0)
可能有一种方法可以在xquery中进行列表聚合,但是我对此并不熟练,这是我所能获得的
select *
from
xmltable( '$doc/persons/person' passing
XMLPARSE(DOCUMENT '<persons>
<person><name>BLA1</name><parameters>
<param><key>KKK1</key><value>VVV1</value></param>
<param><key>KKK2</key><value>VVV2</value></param></parameters></person>
<person><name>BLA2</name><parameters>
<param><key>KKK21</key><value>VVV21</value></param>
<param><key>KKK22</key><value>VVV22</value></param>
<param><key>KKK23</key><value>VVV23</value></param></parameters></person></persons>') as "doc"
COLUMNS "NAME" VARCHAR(64) PATH './name'
, "PARAMS" VARCHAR(64) PATH 'fn:string-join(./parameters/param/key,";")'
, "VALUES" VARCHAR(64) PATH 'fn:string-join(./parameters/param/value,";")'
) as x
返回
NAME PARAMS VALUES
---- ----------------- -----------------
BLA1 KKK1;KKK2 VVV1;VVV2
BLA2 KKK21;KKK22;KKK23 VVV21;VVV22;VVV23
我不确定如何在xquery中将PARAMS和VALUES合并到单个列中
答案 2 :(得分:0)
记录下来,这就是您在视图中使用Mark答案的方式
create table tbl1 (datas xml) organize by row;
INSERT INTO tbl1 VALUES '<persons>
<person><name>BLA1</name><parameters>
<param><key>KKK1</key><value>VVV1</value></param>
<param><key>KKK2</key><value>VVV2</value></param></parameters></person>
<person><name>BLA2</name><parameters>
<param><key>KKK21</key><value>VVV21</value></param>
<param><key>KKK22</key><value>VVV22</value></param>
<param><key>KKK23</key><value>VVV23</value></param></parameters></person></persons>'
;
CREATE VIEW vw1 AS
select NAME
, listagg(key||'='||value, ';') params
from tbl1
, xmltable( '$doc/persons/person/parameters/param' passing datas as "doc"
COLUMNS "NAME" VARCHAR(64) PATH '../../name'
, "KEY" VARCHAR(64) PATH './key'
, "VALUE" VARCHAR(64) PATH './value'
)
group by NAME