title: How to get the middle most record(s) from a group of data in sql
body: <pre><code>create table #middle
  (
  A INT,
  B INT,
  C INT
  )
  
  INSERT INTO #middle (A,B,C) VALUES (7,6,2),(1,0,8),(9,12,16),(7, 16, 2),(1,12,8), (9,12,16),(9,12,16),(7, 16, 2),(1,12,8), (9,12,16) 
  
      ;WITH MIDS
           AS (SELECT *,
                      Row_number()
                        OVER (
                          ORDER BY a, b, c DESC )AS rn
               FROM   #middle)
      SELECT *
      FROM   MIDS
      WHERE  rn &lt;= (SELECT CASE ( Count(*)%2 )
                             WHEN 0 THEN ( Count(*) / 2 ) + 1
                             ELSE ( Count(*) / 2 )
                           END
                    FROM   MIDS) except (SELECT *
                                    FROM   MIDS
                                    WHERE  rn &lt; (SELECT ( Count(*) / 2 )
                                                 FROM   MIDS)) 
  </code></pre>
  
  <p>The query i have tried works >4 records but not for '3'.Now my question is how should i modify my query so that for 3 records i should get the 2nd record which is the middle most record among them,try to insert only 3 records from above records and help. Thanks in advance.</p>