title: Getting latest date in SQL query
body: <p>I am running this query right now: </p>
  
  <pre><code>WITH Result AS (
  
  select distinct
      --pump.Product_PN,
      --pump.product_name,
      mot.Motor_pn,
      Mot.Motor_name,
      mas.STLAN AS Usage,
      po.DATUV as change_date,
      mot.[Procurement type],
      mot.Plant as plant, 
      po.IDNRK AS Component_PN,
      mak.MAKTG as component_name
  
  
  FROM dbo.TBLMAST AS mas
  INNER JOIN dbo.TBLSTPO AS po
      ON mas.STLNR = po.STLNR
  INNER JOIN dbo.TBLMAKT AS mak 
      ON mak.MATNR = po.IDNRK
  
  
  --INNER JOIN ['Pumps to motor$'] AS pump
      --ON pump.[Motor Product Number] = SUBSTRING(mas.MATNR, PATINDEX('%[^0 ]%', mas.MATNR + ' '), LEN(mas.MATNR))
  
  INNER JOIN Grundfos_motors AS mot
      ON mot.Motor_PN = SUBSTRING(mas.MATNR, PATINDEX('%[^0 ]%', mas.MATNR + ' '), LEN(mas.MATNR))
  
  
  where mas.STLAN = '8' and mot.Motor_PN = '78085617' and mak.MAKTG like '%stator%'
  
   )
  
   select motor_pn, motor_name, plant, change_date, Component_PN, Component_name, cast(getdate() as date) as Date_of_Extraction_date
  
   from result
  
  
  
   order by change_date DESC;
  </code></pre>
  
  <p>The result of this query is this:</p>
  
  <p><a href="https://i.stack.imgur.com/Lhc1T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lhc1T.png" alt="Query 1"></a>  </p>
  
  <p>From this query result, I want to run another query where the <code>change_date</code> is set to the latest. So I only want to retrieve the data for the latest date and rest is excluded. Something like:</p>
  
  <p><a href="https://i.stack.imgur.com/ZMImm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZMImm.png" alt="Result"></a></p>
  
  <p>Can anyone help? </p>