T-SQL选择没有活动的帐户

时间:2012-02-20 19:26:00

标签: sql tsql

我有这张桌子

ID  InvoiceNo  Created_date
--  ---------  ------------
1   123        1/1/2009
1   234        1/1/2010
1   2304       2/1/2010
1   av245      3/1/2011
1   45wd3      4/1/2011
2   345        1/1/2010
2   4w5        2/1/2010

我正在尝试选择自11/1/2010以来没有活动的ID。 所以我的结果应该只显示ID 2.

我使用的是EXISTS,不存在,但它仍然显示ID 1.任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

没有针对性能进行优化,但我会做这样的事情: 快速而肮脏的警报

select distinct q.id from (select id, max(created_date) as latestdate from TABLENAME group by id) q where q.latestdate <= '2010-11-01'

答案 1 :(得分:0)

在oracle中我可能会写这样的东西:

select id from mytable
MINUS
SELECT id from mytable where created_dt > '2010-11-01'

使用NOT IN:

select * from mytable 
where id not in ( select id from mytable where created_dt > '2010-11-01' )
相关问题