title: How to get last 5 rows using CTE?
body: <p>I have a problem using a CTE.</p>
  
  <pre><code>WITH CTE AS 
  (
      SELECT 
          ROW_NUMBER() OVER (PARTITION BY #TEMPTABLE.Id order by #TEMPTABLE.Id) AS 'RowNo',
          #TEMPTABLE.Id,
          #TEMPTABLE.UserId,
          #TEMPTABLE.Code,
          #TEMPTABLE.CodeDesc
      FROM #TEMPTABLE
  )
  SELECT DISTINCT Id, UserId,Code,CodeDesc          
  FROM CTE
  where RowNo &lt;=5
  </code></pre>
  
  <p>From this query, I want to get the last 5 rows. But I am always getting first 5 rows. Please give me a solution in this.</p>