title: SQL Server, cross-apply, and DISTINCT
body: <p>I had earlier problem with CROSS APPLY, which solved nicely (<a href="https://stackoverflow.com/questions/31116291/using-cross-apply/">Using CROSS APPLY</a>). </p>
  
  <p>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:</p>
  
  <pre><code>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 &gt; (GETDATE()-1)
  </code></pre>
  
  <p>So, how to make DISTINCT and whole rows work?</p>