我有两个表:Table1
和Table2
。 Table1
列RECORDID
,DATASOURCETYPE
和STRINGCLOB
,Table2
列RECORDID
,TIMESTAMP
和CLICKTYPE
。我编写了一个查询来查找来自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'
。我正在努力从两个不同的表中查询数据,任何想法?
非常感谢,
我
答案 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'