简体   繁体   English

xquery选择属性

[英]xquery select on attributes

There is a table A with date column named logdate and xml column named tag which has xml value like this: 有一个表A ,其日期列名为logdate而xml列名为tag ,其xml值如下所示:

<process id="1540193803286" startTime="2018-10-24 10:01:26.467" >
<user>
<userId>2020</userId>   
</user> 
<executionCclass>
<executionNode classid="1" className="testclass_record1" methodName="testmethod" timeSpent="0" />
<executionNode classid="1-1" className="testclass2_record1" methodName="testmethod2" timeSpent="0" />

I want to select records with specific logdate which has classid="1" in their xml. 我想选择具有特定logdate其xml中具有classid="1"的记录。

example: TableName : TestTable Columns: logdate (Decimal), xml (Xml) 示例:TableName:TestTable列:logdate(十进制),xml(XML)

sample records: 样本记录:

1) logdate=20181101, xml=[something like what I wrote above] 1)logdate = 20181101,xml = [类似于我上面写的内容]

2) logdate=20181101, xml=[something like what I wrote above] 2)logdate = 20181101,xml = [类似于我在上面写的内容]

3) logdate=20181102, xml=[something like what I wrote above] 3)logdate = 20181102,xml = [类似于我上面写的内容]

4) logdate=20181103, xml=[something like what I wrote above] 4)logdate = 20181103,xml = [类似于我上面写的内容]

5) logdate=20181103, xml=[something like what I wrote above] 5)logdate = 20181103,xml = [类似于我上面写的内容]

Result I want: 结果我想要:

className where Logdate>20181101 And classid=1 className其中Logdate> 20181101并且classid = 1

for example: 例如:

1) 20181102, testclass_record3 1)20181102,testclass_record3

2) 20181103, testclass_record4 2)20181103,testclass_record4

3) 20181103, testclass_record5 3)20181103,testclass_record5

How can I use db2 xquery for this select? 如何为此选择使用db2 xquery?

Try this: 尝试这个:

declare global temporary table session.test_xml (logdate dec(8), tag xml) with replace on commit preserve rows not logged;

insert into session.test_xml values 
  (20181102, xmlparse (document '
<process id="1540193803286" startTime="2018-10-24 10:01:26.467" >
  <user>
    <userId>2020</userId>   
  </user> 
  <executionCclass>
    <executionNode classid="1" className="testclass_record1" methodName="testmethod" timeSpent="0" />
    <executionNode classid="1-1" className="testclass2_record1" methodName="testmethod2" timeSpent="0" />
  </executionCclass>
</process>
'
))
, (20181102, xmlparse (document '
<process id="1540193803286" startTime="2018-10-24 10:01:26.467" >
  <user>
    <userId>2020</userId>   
  </user> 
  <executionCclass>
    <executionNode classid="2" className="testclass_record1" methodName="testmethod" timeSpent="0" />
    <executionNode classid="1-1" className="testclass2_record1" methodName="testmethod2" timeSpent="0" />
  </executionCclass>
</process>
'
))
;

select a.logdate, t.classname
from session.test_xml a
, xmltable ('$doc/process/executionCclass/executionNode[@classid="1"]' passing a.tag as "doc" columns
    classname varchar(128) path '@className'
) t 
where xmlexists('$doc/process/executionCclass/executionNode[@classid="1"]' passing a.tag as "doc")
and Logdate>20181101;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM