簡體   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