title: Get records based on latest date
body: <p>In the below query I need to take only one record which is the latest phDate.</p>
  
  <pre><code> WITH TEMP_PAT_PIP_AddedBy
  AS (
      SELECT S.ppId
          ,(
              SELECT PersonID
              FROM [PFMADestination].[dbo].PERSON
              WHERE SourceType = 'user_Users'
                  AND SourceId = S.uuId
              ) AS AddedByToStore
          ,phDate
      FROM [PFMADestination].[dbo].PatientPIP D
      INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
          AND phEvent = 'PIP-info uppdaterad'
      )
  --select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
  UPDATE P
  SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
  FROM TEMP_PAT_PIP_AddedBy T
  INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
  </code></pre>
  
  <p><strong>Expected Output for CTE</strong> Take only latest phDate for each ppId and ignore other if its more than 1 record then update it. </p>
  
  <p><img src="https://i.stack.imgur.com/cavkU.png" alt="enter image description here"></p>