查询包含CLOB数据类型的两个表

时间:2012-12-06 13:08:49

标签: sql oracle clob

我有两个表:Table1Table2Table1RECORDIDDATASOURCETYPESTRINGCLOBTable2RECORDIDTIMESTAMPCLICKTYPE 。我编写了一个查询来查找来自STRINGCLOB列的clob XML字符串'book',前提是它与给定的DATASOURCETYPE匹配,如下所示:

SELECT SUBSTR(stringclob, instr(stringclob, 'type>')+5
                        , instr(stringclob,'</type') 
                             - instr(stringclob, 'type>')-5
             ) r_type from Table1
 where DataSourceType = 'Source1'
   AND SUBSTR(stringclob, instr(stringclob,'type>')+5
                        , instr(stringclob, '</type')
                              - instr(stringclob, 'type>')-5
             )
     like '%book%'

现在,我想查找并计算上个月记录的STRINGCLOB列(Table1)中所有'%book%'字符串的出现次数(类似于AND {{1 }和} TIMESTAMP > '01-Nov-12' AND TIMESTAMP < '01-Dec-12'。我正在努力从两个不同的表中查询数据,任何想法?

非常感谢,

1 个答案:

答案 0 :(得分:2)

这是获取值为book的record_type计数的方法。

WITH t
       AS (SELECT   '<?xml version="1.0" encoding="UTF-8"?>
                       <record>
                        <record_number>12345</record_number>
                         <record_type> book </record_type>
                       </record>'
                       STRINGCLOB
             FROM   DUAL
           UNION ALL
           SELECT   '<?xml version="1.0" encoding="UTF-8"?>
                          <record>
                            <record_number>6789</record_number>
                            <record_type> book </record_type>
                         </record>'
                       STRINGCLOB
             FROM   DUAL)
SELECT   COUNT (1) total
  FROM   t
 WHERE   xmltype (t.STRINGCLOB).EXTRACT ('//record_type/text()') LIKE '%book%';

2

现在,如果你想加入表2,基于record_id

 SELECT   COUNT (1) total
   FROM   table1 t1, table2 t2
  WHERE   xmltype (t1.STRINGCLOB).EXTRACT ('//record_type/text()') LIKE
            '%book%'
         AND t1.record_id = t2.record_id
         AND t1.DataSourceType = 'Source1'
         AND (TRUNC (t2.timestamp) > '01-Nov-12'
              AND TRUNC (t2.timestamp) < '01-Dec-12')
         AND t2.clicktype <> 'Type1'