Oracle-选择查询中的XMLType列比较

时间:2019-02-13 23:33:28

标签: sql xml oracle oracle11g xmltype

我的数据库架构如下:

ID  IDLANE  COMPONENT (XMLType COlumn)                  IDDATE  
--- ------  ---------                                   ------
111 111R1   <?xml version="1.0" encoding="utf-8"?>      2019-02-10T10:00:00
            <Rowsets>
            <Rowset Name="Item">
            <Row>
                <IDLANE>111R1</IDLANE>
                <ID>111</ID>
                <ORDER>0111000</ORDER>
                <ITEM>000010</ITEM>
                <MATERIAL>000000000257</MATERIAL>
            </Row>
            <Row>
                <IDLANE>111R1</IDLANE>
                <ID>111</ID>
                <ORDER>0111000</ORDER>
                <ITEM>000020</ITEM>
                <MATERIAL>000000000258</MATERIAL>
            </Row>
            </Rowset>
            </Rowsets>



111 111R2   <?xml version="1.0" encoding="utf-8"?>      2019-02-10T11:00:00
            <Rowsets>
            <Rowset Name="Item">
            <Row>
                <IDLANE>111R2</IDLANE>
                <ID>111</ID>
                <ORDER>0111000</VBELN>
                <ITEM>000010</ORDER>
                <MATERIAL>000000000257</MATERIAL>
            </Row>
            <Row>
                <IDLANE>111R2</IDLANE>
                <ID>111</ID>
                <ORDER>0111000</ORDER>
                <ITEM>000020</ITEM>
                <MATERIAL>000000000258</MATERIAL>
            </Row>
            </Rowset>
            </Rowsets>


222 222R1   <?xml version="1.0" encoding="utf-8"?>      2019-02-10T13:00:00
            <Rowsets>
            <Rowset Name="Item">
            <Row>
                <IDLANE>222R1</IDLANE>
                <ID>222</ID>
                <ORDER>0222000</ORDER>
                <ITEM>000010</ITEM>
                <MATERIAL>000000000257</MATERIAL>
            </Row>
            <Row>
                <IDLANE>222R1</IDLANE>
                <ID>111</ID>
                <ORDER>0222000</ORDER>
                <ITEM>000020</ITEM>
                <MATERIAL>000000000258</MATERIAL>
            </Row>
            </Rowset>
            </Rowsets>  

222 222R2   <?xml version="1.0" encoding="utf-8"?>      2019-02-10T14:00:00
            <Rowsets>
            <Rowset Name="Item">
            <Row>
                <IDLANE>222R2</IDLANE>
                <ID>222</ID
                <ORDER>0222000</ORDER>
                <ITEM>000010</ITEM>
                <MATERIAL>000000000259</MATERIAL
            </Row>

            </Rowset>
            </Rowsets>  


333 333R1   <?xml version="1.0" encoding="utf-8"?>      2019-02-10T18:00:00
            <Rowsets>
            <Rowset Name="Item">
            <Row>
                <IDLANE>333R1</IDLANE>
                <ID>333</ID
                <ORDER>0333000</ORDER>
                <ITEM>000010</ITEM>
                <MATERIAL>000000000259</MATERIAL
            </Row>

            </Rowset>
            </Rowsets>  

我的要求是获取所有的ID:

exists more than one AND 
<ORDER> is same for all of them AND 
<ITEM> is same for all of them AND  
<MATERIAL> is same for all of them

对于我提供的数据,查询输出应为:

ID    IDLANE    IDDATE
111   111R1     2019-02-10T10:00:00
111   111R2     2019-02-10T11:00:00

It wont include 222 because both 222 IDs have different Material.

我编写了查询以标识多个ID,如下所示:

SELECT ID FROM IDTABLE 
GROUP BY ID
HAVING COUNT(*) > 1

以上查询为我提供了111和222的答案,因为两个ID存在多个。但是我需要比较XMLTyple COlumn的,并且每个相同的ID也会出现两次。但是我不知道如何比较XML类型列。

任何人都可以提供一些输入信息。

谢谢

1 个答案:

答案 0 :(得分:1)

我想您需要在感兴趣的列中具有相同数据的行,但在IDDATE列中具有不同的值。

with
  t as (
    select
      111 as ID, '111R1' as IDLANE,
      xmltype(q'~
<Rowsets>
  <Rowset Name="Item">
    <Row>
      <IDLANE>111R1</IDLANE>
      <ID>111</ID>
      <ORDER>0111000</ORDER>
      <ITEM>000010</ITEM>
      <MATERIAL>000000000257</MATERIAL>
    </Row>
    <Row>
      <IDLANE>111R1</IDLANE>
      <ID>111</ID>
      <ORDER>0111000</ORDER>
      <ITEM>000020</ITEM>
      <MATERIAL>000000000258</MATERIAL>
    </Row>
  </Rowset>
</Rowsets>
~') as COMPONENT, timestamp'2019-02-10 10:00:00' as IDDATE from dual
    union all
    select
      111, '111R2',
      xmltype(q'~
<Rowsets>
  <Rowset Name="Item">
    <Row>
      <IDLANE>111R2</IDLANE>
      <ID>111</ID>
      <ORDER>0111000</ORDER>
      <ITEM>000010</ITEM>
      <MATERIAL>000000000257</MATERIAL>
    </Row>
    <Row>
      <IDLANE>111R2</IDLANE>
      <ID>111</ID>
      <ORDER>0111000</ORDER>
      <ITEM>000020</ITEM>
      <MATERIAL>000000000258</MATERIAL>
    </Row>
  </Rowset>
</Rowsets>
~'), timestamp'2019-02-10 11:00:00' from dual
    union all
    select
      222, '222R1',
      xmltype(q'~
<Rowsets>
  <Rowset Name="Item">
    <Row>
      <IDLANE>222R1</IDLANE>
      <ID>222</ID>
      <ORDER>0222000</ORDER>
      <ITEM>000010</ITEM>
      <MATERIAL>000000000257</MATERIAL>
    </Row>
    <Row>
      <IDLANE>222R1</IDLANE>
      <ID>111</ID>
      <ORDER>0222000</ORDER>
      <ITEM>000020</ITEM>
      <MATERIAL>000000000258</MATERIAL>
    </Row>
  </Rowset>
</Rowsets>
~'), timestamp'2019-02-10 13:00:00' from dual
    union all
    select
      222, '222R2',
      xmltype(q'~
<Rowsets>
  <Rowset Name="Item">
    <Row>
      <IDLANE>222R2</IDLANE>
      <ID>222</ID>
      <ORDER>0222000</ORDER>
      <ITEM>000010</ITEM>
      <MATERIAL>000000000259</MATERIAL>
    </Row>
  </Rowset>
</Rowsets>
~'), timestamp'2019-02-10 14:00:00' from dual
    union all
    select
      333, '333R1',
      xmltype(q'~
<Rowsets>
  <Rowset Name="Item">
    <Row>
      <IDLANE>333R1</IDLANE>
      <ID>333</ID>
      <ORDER>0333000</ORDER>
      <ITEM>000010</ITEM>
      <MATERIAL>000000000259</MATERIAL>
    </Row>
  </Rowset>
</Rowsets>
~'), timestamp'2019-02-10 18:00:00' from dual
  ),
  a as (
    select
      t."ID", t.IDLANE, t.IDDATE,
      count(*) over (
        PARTITION BY t."ID", x."order", x.ITEM, x.MATERIAL
      ) as qnt
    from t,
      xmltable('//Row'
        passing t.component
        columns
          "order" varchar2(10) path 'ORDER',
          item varchar2(10) path 'ITEM',
          material varchar2(20) path 'MATERIAL') x
  )
select distinct
  "ID", IDLANE, to_char(IDDATE, 'yyyy-MM-dd hh24:mi:ss') as IDDATE
from a where qnt > 1;

输出:

+-----+--------+---------------------+
| ID  | IDLANE |       IDDATE        |
+-----+--------+---------------------+
| 111 | 111R2  | 2019-02-10 11:00:00 |
| 111 | 111R1  | 2019-02-10 10:00:00 |
+-----+--------+---------------------+

使用db<>fiddle在线进行测试。