[英]Oracle: How do I join two result set queries (based on the same table) and then subtract the results?
My problem我的问题
I have two queries that have the same select and from criteria but have a different where statement.我有两个查询,它们具有相同的 select 和 from 条件,但具有不同的 where 语句。 Each query counts the number of 'actions'.
每个查询都计算“操作”的数量。 The first query counts all files created, while the other query counts all files that have been deleted.
第一个查询计算所有创建的文件,而另一个查询计算所有已删除的文件。 To get the updated file count, I need to join them and then subtract the result set count for deleted from the result set count for created.
要获得更新的文件计数,我需要加入它们,然后从创建的结果集计数中减去删除的结果集计数。
Here are my two queries.这是我的两个查询。 They are essentially the same except for one of them has table2.auditid = 15 for created and the other has table2.auditid = 14 for deleted.
它们本质上是相同的,除了其中一个具有 table2.auditid = 15 用于创建,另一个具有 table2.auditid = 14 用于删除。
Created:创建:
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
Deleted:删除:
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
Note that these queries run well on their own.请注意,这些查询本身运行良好。
What I've Tried我试过的
Here's the code I adapted:这是我改编的代码:
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
Can anyone provide some insight ?任何人都可以提供一些见解吗?
In your "Deleted" query block you are still giving the column name "Files Created"
in the SELECT
list;在您的“已删除”查询块中,您仍然在
SELECT
列表中提供列名称"Files Created"
; I assume that is a mistake, it should be "Files Deleted"
, right?我认为这是一个错误,应该是
"Files Deleted"
,对吗?
To answer your question: It appears that you recognize files that are "created" vs. "deleted" by the table2.auditid
attribute, correct?回答您的问题:您似乎可以识别由
table2.auditid
属性“创建”与“删除”的文件,对吗? 15 for created, 14 for deleted? 15 表示创建,14 表示删除?
To capture both in a single query, that part of the last group of where
conditions should become要在单个查询中捕获两者,最后一组
where
条件的那部分应该变成
... where table2.auditid in (14, 15) and ...
Then you only need to change the aggregate function in the outer select
- it needs to be a sum
, and a conditional sum at that.然后您只需要更改外部
select
的聚合函数 - 它需要是一个sum
,并且是一个条件和。
count(table1.id)
counts non-null values. count(table1.id)
计算非空值。 I assume the id can't be null, so that is the same as count(*)
- or, even, sum(1)
.我假设 id 不能为空,所以这与
count(*)
相同 - 或者甚至sum(1)
。 This will help with the current assignment: what you need instead of sum(1)
when you want to add 1 for each table2.auditid = 15
but subtract 1 for each table2.auditid = 14
is:这将有助于当前的分配:当您想为每个
table2.auditid = 15
添加 1 但为每个table2.auditid = 14
减去1 时,您需要什么而不是sum(1)
是:
sum(decode(table2.auditid, 15, +1, 14, -1)) [as <whatever>]
Good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.