繁体   English   中英

Oracle:如何加入两个结果集查询(基于同一张表)然后减去结果?

[英]Oracle: How do I join two result set queries (based on the same table) and then subtract the results?

我的问题

我有两个查询,它们具有相同的 select 和 from 条件,但具有不同的 where 语句。 每个查询都计算“操作”的数量。 第一个查询计算所有创建的文件,而另一个查询计算所有已删除的文件。 要获得更新的文件计数,我需要加入它们,然后从创建的结果集计数中减去删除的结果集计数。

这是我的两个查询。 它们本质上是相同的,除了其中一个具有 table2.auditid = 15 用于创建,另一个具有 table2.auditid = 14 用于删除。

创建:

SELECT decode(table1.id,
2984, 'Category 1',
3298, 'Category 2',
2390, 'Category 3',
4039, 'Category 4',
5048, 'Category 5',
'Unknown') "Category",
COUNT (table1.id) AS "Files Created"
FROM table1
JOIN
maintable ON maintable.dataid = table1.id
JOIN 
table2 ON table2.dataid = maintable.id
JOIN
table3 ON table3.id = table2.userid
WHERE table2.auditid = 15
AND auditdate >= %1
AND table2.subtype = 0
AND table1.subtype = -18
GROUP BY table1.id

删除:

SELECT decode(table1.id,
2984, 'Category 1',
3298, 'Category 2',
2390, 'Category 3',
4039, 'Category 4',
5048, 'Category 5',
'Unknown') "Category",
COUNT (table1.id) AS "Files Created"
FROM table1
JOIN
maintable ON maintable.dataid = table1.id
JOIN 
table2 ON table2.dataid = maintable.id
JOIN
table3 ON table3.id = table2.userid
WHERE table2.auditid = 14
AND auditdate >= %1
AND table2.subtype = 0
AND table1.subtype = -18
GROUP BY table1.id

请注意,这些查询本身运行良好。


我试过的

  • 我不能使用减号语句,因为它不适用于结果集(我忘记了这一点,之前已经问过这个问题)
  • 我试图将这两个查询嵌套为子查询并使用联合等,但无法使其工作。
  • 我已经尝试过 ( SQL sum 2 different column by different condtion then subtraction and add ) 中描述的方法。 这给了我错误 ORA-00904 String Invalid Identifier "table1.id.

这是我改编的代码:

select decode(table1.id,
2984, 'Category 1',
3298, 'Category 2',
2390, 'Category 3',
4039, 'Category 4',
5048, 'Category 5',
'Unknown') "Category", 
(filescreated.CNT - filesdeleted.CNT) as "Final Count", 
from (
 SELECT table1.id, 
 COUNT(table1.id) as CNT
 FROM table1
 JOIN
 maintable ON maintable.dataid = table1.id
 JOIN 
 table2 ON table2.dataid = maintable.id
 JOIN
 table3 ON table3.id = table2.userid
 WHERE table2.auditid = 15
 AND auditdate >= %1
 AND table2.subtype = 0
 AND table1.subtype = -18
 GROUP BY table1.id) filescreated,
    (SELECT table1.id,
    COUNT(llattrdata.defid) as CNT
    FROM table1
    JOIN
    maintable ON maintable.dataid = table1.id
    JOIN 
    table2 ON table2.dataid = maintable.id
    JOIN
    table3 ON table3.id = table2.userid
    WHERE table2.auditid = 14
    AND auditdate >= %1
    AND table2.subtype = 0
    AND table1.subtype = -18
    GROUP BY table1.id) filesdeleted

ORDER BY table1.id

任何人都可以提供一些见解吗?

在您的“已删除”查询块中,您仍然在SELECT列表中提供列名称"Files Created" 我认为这是一个错误,应该是"Files Deleted" ,对吗?

回答您的问题:您似乎可以识别由table2.auditid属性“创建”与“删除”的文件,对吗? 15 表示创建,14 表示删除?

要在单个查询中捕获两者,最后一组where条件的那部分应该变成

... where table2.auditid in (14, 15)  and   ...

然后您只需要更改外部select的聚合函数 - 它需要是一个sum ,并且是一个条件和。

count(table1.id)计算非空值。 我假设 id 不能为空,所以这与count(*)相同 - 或者甚至sum(1) 这将有助于当前的分配:当您想为每个table2.auditid = 15添加 1 但为每个table2.auditid = 14减去1 时,您需要什么而不是sum(1)是:

sum(decode(table2.auditid, 15, +1, 14, -1))   [as <whatever>]

祝你好运!

暂无
暂无

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

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