【问题标题】:Oracle: How do I join two result set queries (based on the same table) and then subtract the results?Oracle:如何加入两个结果集查询(基于同一张表)然后减去结果?
【发布时间】:2018-11-02 07:27:54
【问题描述】:

我的问题

我有两个查询具有相同的 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

谁能提供一些见解?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

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

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

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

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

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

    count(table1.id) 计算非空值。我假设 id 不能为 null,因此它与 count(*) 相同 - 甚至是 sum(1)。这将有助于当前的分配:当您想为每个 table2.auditid = 15 添加 1 但 subtract 每个 table2.auditid = 14 1 时,您需要的不是 sum(1) 是:

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

    祝你好运!

    【讨论】:

    • -是的,我的意思是第二个块上的“文件已删除”,这只是复制时的错误
    • -我刚刚修复了我的查询,它运行良好!非常感谢!
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 2019-11-07
    • 2011-09-16
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2014-04-15
    • 2017-04-05
    相关资源
    最近更新 更多