declare @tab table
(
BatchID INT,
Code VARCHAR(20),
CommType INT,
LastStatus VARCHAR(5),
SourceModiifedLastDate varchar(30)
)
INSERT INTO @tab(BatchID, Code, CommType, LastStatus, SourceModiifedLastDate)
VALUES (1, 'A003-3', 3, 'I', '2013-06-17 21:28:01.827'),
(2, 'A004-1', 1, 'I', '2014-06-17 21:28:01.827'),
(6, 'A003-3', 3, 'U', '2015-06-17 21:28:01.827'),
(9, 'A003-3', 3, 'D', '2015-06-17 21:28:01.827'),
(11, 'A004-1', 3, 'D', '2013-06-17 21:28:01.827'),
(12, 'A004-1', 1, 'I', '2015-06-17 21:28:01.827'),
(16, 'A005-3', 3, 'I', '2011-06-17 21:28:01.827'),
(19, 'A005-3', 3, 'D', '2013-0617 21:28:01.827'),
(20, 'A006-3', 3, 'U', '2011-06-17 21:28:01.827'),
(21, 'A006-3', 3, 'I', '2013-0617 21:28:01.827')
Select * from @tab
在我的示例數據中,我只需要獲取Laststatus ='D'記錄根據最新BatchID。如何獲得基於批號的最新記錄
例如,如果你看到的代碼=「A003-3」它得到了插入,更新和刪除,我需要得到這個紀錄
如果你看到的代碼=「A004-1」它得到了插入,刪除和插入我不需要這個記錄。
輸出應該是:
BatchID Code CommType LastStatus SourceModiifedLastDate
---------------------------------------------------------------
9 A003-3 3 D 2015-06-17 21:28:01.827
19 A005-3 3 D 2013-06-17 21:28:01.827
我需要根據最新BatchID
和最新的日期只得到最新刪除的記錄。
我試過使用MAX
條件和GROUP BY
來過濾記錄,但我無法得到我在找什麼。
請大家幫我想想辦法
有道理謝謝 – mohan111