SELECT T1.Dogovor_Id, T1.DataStatusChanged As LastData, Max(T2.Status_ID) AS LastStatus_Id
FROM Query1 AS T2 INNER JOIN (SELECT T1.Dogovor_Id, Max(T1.DataStatusChanged) AS DataStatusChanged
FROM Query1 AS T1 GROUP BY T1.Dogovor_Id) AS T1
ON (T2.DataStatusChanged = T1.DataStatusChanged) AND (T2.Dogovor_Id = T1.Dogovor_Id)
GROUP BY T1.Dogovor_Id, T1.DataStatusChanged;
|