2014-03-27 37 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