我有两个独立的Oracle SQL查询,可以独立工作, 但我无法弄清楚如何将它们合二为一。
查询一:来自4个表的字段的连接。
SELECT
table01.guid,
table02.kbcid,
table01.name,
table01.filename,
table01.filesize,
table01.essencegroup,
table01.checksum,
table01.type,
table02.kbcid,
table03.kbcnm,
table03.parentkbcid,
table03.kbcpath,
table04.metaid,
table04.metavalue,
FROM table02
INNER JOIN table01 ON table02.guid=table01.guid
INNER JOIN table03 ON table02.kbcid=table03.kbcid
INNER JOIN table04 ON table01.guid=table04.guid;
查询二:具有一对多关系的表,使用LISTAGG连接行。
SELECT
table5.guid,
LISTAGG(TO_CHAR(table5.term), ',') WITHIN GROUP (ORDER BY table5.guid) AS
keywords
FROM table5
Group BY table5.guid;
理想情况下,我想结合这两个查询来返回一组结果。 但我不断收到各种错误。
有人可以建议格式化此查询的正确方法吗?
答案 0 :(得分:0)
我不知道表之间的关系,但我认为你可以使用WITH子句!这是一个例子,用表关系修复它:
WITH tab5 AS (
SELECT
table5.guid,
LISTAGG(TO_CHAR(table5.term), ',') WITHIN GROUP (ORDER BY table5.guid) AS
keywords
FROM table5
GROUP BY table5.guid
)
SELECT
table01.guid,
table02.kbcid,
table01.name,
table01.filename,
table01.filesize,
table01.essencegroup,
table01.checksum,
table01.type,
table02.kbcid,
table03.kbcnm,
table03.parentkbcid,
table03.kbcpath,
table04.metaid,
table04.metavalue,
FROM table02
INNER JOIN table01 ON table02.guid=table01.guid
INNER JOIN table03 ON table02.kbcid=table03.kbcid
INNER JOIN table04 ON table01.guid=table04.guid
--Put relation to other tables
INNER JOIN tab5 ON tabl01.guid = tab5.guid;