title: Select top 50 rows in sql server
body: <p>I have below T-SQL code used in SQL Server 2014.</p>
  
  <p>This code produces 1000s of rows. But i need only top 50 rows (from Supplier Column) from this.</p>
  
  <p>In the below code, if I use <code>SELECT Top 50 s.[CusNo] Supplier</code> then I am not getting desired results.</p>
  
  <p>What changes needs to be done in the below code in order to get only Top 50 rows (of Supplier column) with out any change in the existing result.</p>
  
  <pre><code>SELECT s.[CusNo] Supplier, 
  RTRIM(CAST(s.[Customer] AS VARCHAR(50)) ) AS Name,
  s.[ConcessionNo] Concession, 
  RTRIM(CAST(s.[ConcessionName] AS VARCHAR(50)) ) AS ConcessionName,
  
  sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5) 
             then s.SELLINC else 0 end) ActualSales,
  
       sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
             then s.SELLINC else 0 end) LastYrVariance,
  
       (sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5) then s.SELLINC else 0 end))-
  
       (sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */) then s.SELLINC else 0 end)) LastYrVariancePounds,
  
       (IsNull(sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) - 1) and convert(date,dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5) 
             then s.SELLINC else 0 end)-sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
             then s.SELLINC else 0 end),0)/NullIf(sum(case when s.Date between convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) -1 /* On 14/Feb/2021 modify +6 to -1 */) and convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
             then s.SELLINC else 0 end),0))*100 LastYrVariancePercentage,
  
  
      sum(case when s.Date 
          BETWEEN         
               convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND        
               Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)       
             then s.SELLINC else 0 end) YrToDateActual,
  
  
      sum(case when s.Date    
          BETWEEN  
             convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND
             convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
            then s.SELLINC else 0 end) LastYrToDateActual,
  
  
      (sum(case when s.Date 
       BETWEEN        
               convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND        
               Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5) 
             then s.SELLINC else 0 end))  
  
       -
  
      (sum(case when s.Date 
          BETWEEN  
             convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND
             convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
             then s.SELLINC else 0 end)) YrToDateVariancePounds,
  
  
      ((IsNull    
           (                   
              (sum(case when s.Date 
               BETWEEN        
               convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(GetDate()) = 1 THEN CONVERT(VARCHAR(4), GetDate(), 112) - 1 ELSE CONVERT(VARCHAR(4), GetDate(), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND        
               Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5)
             then s.SELLINC else 0 end))  
  
       -
  
      (sum(case when s.Date 
          BETWEEN  
             convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND
             convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
             then s.SELLINC else 0 end))
  
            ,0)
  
        ) 
  
        /
  
      (NullIf 
        (
           sum(case when s.Date 
              BETWEEN  
             convert(varchar(10), DATEADD(day, DATEDIFF(day, '19000107', DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE WHEN MONTH(DATEADD(year,-1,GetDate())) = 1 THEN CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) - 1 ELSE CONVERT(VARCHAR(4), DATEADD(year,-1,GetDate()), 112) END), 112) + '0101')), 30)) / 7  * 7, '19000107'), 120)
          AND
             convert(date,dateadd(wk, datediff(wk, 0, dateadd(YEAR, - 1, getdate())) - 1, 0) + 5 /* On 14/Feb/2021 modify +12 to +5 */)
             then s.SELLINC else 0 end)          
  
         ,0)))*100 LastYrToDateVariancePercentage
  
  FROM [dbo].[CustomerReports] s
  WHERE s.BRN = 1 or s.BRN = 2 or s.BRN = 3 or s.BRN = 4 or s.BRN = 5  or s.SELLINC is null or s.SELLINC = '0'
  GROUP BY s.[CusNo], s.[Customer], s.ConcessionNo, s.ConcessionName
  order by YrToDateActual desc  
  </code></pre>
  
  <p>When I run this Query In SSRS I get below results (Which is Correct). But displays all the data.</p>
  
  <p><a href="https://i.stack.imgur.com/fkTHf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fkTHf.png" alt="enter image description here"></a></p>
  
  <p>if I use SELECT Top 50 s.[CusNo] Supplier (Rest of the code is same) then I am getting the below,</p>
  
  <p><a href="https://i.stack.imgur.com/HYA6A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HYA6A.png" alt="enter image description here"></a></p>
  
  <p>So please see the highlighted section in both the images. When I add Top 50 in SQL code then few rows (in 3rd column ) which were present in 1st image is not present in 2nd image.</p>