title: MSSQL: How to display TOP 10 items from GROUP BY query?
body: <p>I have a query which displays this as result:</p>
  
  <pre><code>Year-Month  SN_NAME Raised Incidents
  2015-11 A   14494
  2015-11 B   8432
  2015-11 D   5496
  2015-11 G   4778
  2015-11 H   4554
  2015-11 C   4203
  2015-11 X   3477
  .......+ thousands more rows for 2015-11
  2015-12 A   3373
  2015-12 B   3322
  2015-12 H   2814
  2015-12 D   2745
  ......+ thousands more rows for 2015-12
  ......+ thousands more rows for 2016-01 - 2016-10
  2016-11 B   2645
  2016-11 C   2571
  2016-11 E   2475
  2016-11 D   2466
  ....+ thousands more rows for 2016-11
  </code></pre>
  
  <p>I need to select TOP 10 SN_NAME by Raised_Incident count from last month and and then show their COUNTS for previous 12 months.</p>
  
  <p>The query I use to display above result is this one:</p>
  
  <pre><code>DECLARE @startOfCurrentMonth DATETIME
  SET @startOfCurrentMonth = DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)
  
  SELECT 
      CONVERT(char(7),IM.SN_SYS_CREATED_ON,121) as "Year-Month"
      ,CI.SN_NAME
      ,COUNT(IM.SN_NUMBER) as "Raised Incidents"
  FROM [dbo].[tab_IM_Incident] IM 
      LEFT JOIN [dbo].[tab_SNOW_CMDB_CI] CI on IM.SN_CMDB_CI = CI.SN_SYS_ID
  WHERE 
      IM.SN_SYS_CREATED_ON &gt;= DATEADD(month, -13, @startOfCurrentMonth) AND    IM.SN_SYS_CREATED_ON &lt; @startOfCurrentMonth
      AND (IM.SN_U_SUB_STATE &lt;&gt; 'Cancelled' OR IM.SN_U_SUB_STATE IS NULL)
  GROUP BY 
      CONVERT(char(7),IM.SN_SYS_CREATED_ON,121)
      , CI.SN_NAME
  ORDER BY 
      CONVERT(char(7),IM.SN_SYS_CREATED_ON,121) 
      , COUNT(IM.SN_NUMBER) DESC
  </code></pre>
  
  <p>The problem is I don't know how to limit each month values to TOP10 only, as the query returns me around 200 000 rows in total, while it should return 13x10 = 130 rows.</p>
  
  <p>The expected output is exactly as on top of the question, but limited to only top 10 rows per month for last 13 months.</p>
  
  <p>Please advise.</p>