2016-09-01 29 views
0

我有了我需要安排,像這樣的所有信息數據庫表中的最大最後修改日期:T-SQL找到一個組中的所有記錄與超過特定閾值

Inventory_ID | Dealer_ID | LastModifiedDate

每個Dealer_ID都附加到多個Inventory_ID。我需要的是一個查詢計算每個經銷商ID的最大值LastModifiedDate,然後給我一個所有Dealer_ID的列表,其中包含最近30天以後的最後修改日期。

獲取每個Dealer_ID最大上次修改的日期很簡單,當然是:由MAX(LastModifiedDate)Dealer_ID爲了

記錄的保存條件下超過30天

Select Dealer_ID, Max(LastModifiedDate)as MostRecentUpdate 

從清單組也很簡單:

LastModifiedDate < getdate() - 30 

不知何故,我只是不能找出一種方法來結合兩個正常工作。

回答

2

使用HAVING

Select Dealer_ID, Max(LastModifiedDate)as MostRecentUpdate 
from Inventory 
group by Dealer_ID 
having LastModifiedDate < getdate() - 30 
order by MAX(LastModifiedDate) 
+0

完美工作,除了一兩件事,我需要MAX(LastModifiedDate)具有子句。謝謝。 – liampboyle

0

入住此查詢:

Select DT.DealerID, DT.MostRecentUpdate 
(Select DealerID, Max(LastModifiedDate)as MostRecentUpdate 
From YourTable 
Group BY DealerID) DT 
where DT.MostRecentUpdate < GETDATE() - 30 
相關問題