2012-02-20 78 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')