I had earlier problem with CROSS APPLY, which solved nicely (Using CROSS APPLY).
Here is another part. I want to get one newest CarData row (all columns!) for all those cars which have new line within 24 hours. As I assume, first SELECT should get distinct CarIndexes for all those cars which have updated events within 24 hours, and second SELECT gives newest row. Anyhow I dont know where to put DISTINCT word properly, now I just get the list of CarIndexes instead of CarData rows using this query:
SELECT DISTINCT CD.CarIndex FROM [dvm_data].[dbo].[CarData] CD
CROSS APPLY (
SELECT TOP 1 * FROM [dvm_data].[dbo].[CarData] WHERE CarIndex = CD.CarIndex ORDER BY ID DESC) MD
WHERE CD.DatetimeOccurred > (GETDATE()-1)
So, how to make DISTINCT and whole rows work?