title: Remove duplicate records based on timestamp
body: <p>I'm writing a query to find duplicate records. I have table with following columns</p>
  
  <pre><code>Id, Deliveries, TankId, Timestamp.
  </code></pre>
  
  <p>I have inserted duplicate records, that is for same tankid, same deliveries with the +1 day offset timestamp.</p>
  
  <p>Now I want to remove duplicate records which is with lesser timestamp.</p>
  
  <p>e.g. I have duplicate deliveries added for same tankid on 24th and 25th july. I need to remove 24th record.</p>
  
  <p>I tried the following query;</p>
  
  <pre><code>SELECT raw.TimeStamp,raw.[Delivery],raw.[TankId]
  FROM  [dbo].[tObservationData] raw
  INNER JOIN (
      SELECT [Delivery],[TankSystemId]
      FROM [dbo].[ObservationData] 
      GROUP BY [Delivery],[TankSystemId]
      HAVING COUNT([ObservationDataId]) &gt; 1
      ) dup 
      ON raw.[Delivery] = dup.[Delivery] AND raw.[TankId] = dup.[TankId]
      AND  raw.TimeStamp &gt;'2019-06-30 00:00:00.0000000' AND raw.[DeliveryL]&gt;0
  ORDER BY [TankSystemId],TimeStamp
  </code></pre>
  
  <p>But above gives other records too, how can I find and delete those duplicate records?</p>