2
我感到以下邏輯。每當狀態失敗時,我需要在max(EndTime)
之前成功。例如在以下數據中,對於Batch = 3987
,lastrefreshdate
應該是23-01-2017
而不是13-02-2017
。TSQL - 獲取最後成功日期
下面是數據和查詢的片段。
Batch TableName IsFullLoad Status EndTime LastRefreshDate
5524 SA 1 Loaded 13-02-2017 13-02-2017
4746 SA 1 Loaded 06-02-2017 06-02-2017
4174 SA 1 Loaded 31-01-2017 31-01-2017
3987 SA 1 Failed 30-01-2017 13-02-2017
3372 SA 1 Loaded 23-01-2017 23-01-2017
2651 SA 1 Loaded 16-01-2017 16-01-2017
1987 SA 1 Loaded 09-01-2017 09-01-2017
1379 SA 1 Loaded 02-01-2017 02-01-2017
835 SA 1 Loaded 26-12-2016 26-12-2016
189 SA 1 Loaded 19-12-2016 19-12-2016
52 SA 1 Loaded 17-12-2016 17-12-2016
SQL:
SELECT TOP 40*
FROM (
SELECT
bi.Id,
bi.TableName,
bi.IsFullLoad,
bi.JobId,
[Status],
bi.CompletTime,
CASE
WHEN bi.IsFullLoad = 1 AND bi.[Status] = 'Failed'
THEN
(
SELECT MAX(CompletTime) FROM Audit.T1 bb
WHERE [Status]= 'Loaded' AND bb.IsFullLoad = 1 AND bb.TableName = bi.TableName
GROUP BY bb.TableName
)
ELSE
bi.CompletTime
END AS Validto
FROM Audit.T1 bi
WHERE TableName = 'Salesforce_Account' AND bi.IsFullLoad = 1
) x
ORDER BY x.CompletTime desc
任何幫助非常感謝。 在此先感謝。