2014-03-27 35 views
0

我想寫一個查詢查詢查找哪些報告今天尚未生成,但昨天生成?

查找什麼是今天還沒有生成的報告,但昨天生成。

我試過這個,但是這不起作用。

select * from records where name not in 
(Select * from records where rdate to_date('26-03-2014','DD-MM-YYYY')) and 
and rdate=to_date('27-03-2014','DD-MM-YYYY') 

SqlFiddle link

我應該得到的輸出與記錄aadd

+0

謝謝大家。不知道接受哪個答案是正確的。 – user3408958

回答

1

請嘗試:

select 
    * 
from 
    records 
where 
    rdate=to_date('26-03-2014','DD-MM-YYYY') and 
    name not in (Select name from records where rdate=to_date('27-03-2014','DD-MM-YYYY')) 
+0

爲什麼我的工作不正常? – user3408958

+0

語法錯誤存在; '不在'接受只有1列,但你指定'*'和查詢在子查詢中缺少'='。 – TechDo

1

一個left outer join最好not in的情況下是這樣的:

select * from records r1 
    left join records r2 on 
    r1.name = r2.name and 
    r2.rdate = to_date('27-03-2014','DD-MM-YYYY') 
    where r1.rdate = to_date('26-03-2014','DD-MM-YYYY') and r2.rdate is null 

這發現這一切都運行昨天的報告,然後加入今天已運行報告。如果使用where r2.rdate is null,則會排除與今天的報告相匹配的行,因此您只剩下昨天運行但未運行的行。

這樣效率更高,因爲not in重複執行子查詢,而這隻會執行一個查詢。

SQLFiddle