2012-02-20 79 views
2

T-SQL选择账户我有这个表没有活动

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 

我想选择,因为2010年11月1日在那里一直没有活动的ID。 所以我的结果应该只显示ID 2.

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

+0

您与您之间存在不匹配r样本数据和您的期望。 “我的结果应该只显示ID 2”。但是您的ID为1,并且在2011年4月1日创建了InvoiceNo 45wd3,这是2010年11月1日最近的一次。你可能错过了今年10/11的差异。 – 2012-02-20 19:33:15

回答

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' 
+0

谢谢你这个工作。 – Alex 2012-02-20 20:06:17

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')