title: SQL Server Group By Query Select first row each group
body: <p>I am trying to do this query. This is what I have.</p>
  
  <p>My table is: Table</p>
  
  <pre><code>StudyID FacultyID Year    Access1   Access2    Access3
  1          1       2014       4        8          5
  1          2       2014       8        4          7
  1          1       2013       5        4          4
  2          3       2014       4        6          5
  2          5       2013       5        8         10
  2          4       2014       5        5          7
  3          7       2013       9        4          7
  </code></pre>
  
  <p>I want to group by StudyID and Year and get the minimum value of each field Access1 Access2 and Access3 and show only the last year, I mean for each group the first row.
  Here is the Result.</p>
  
  <pre><code>StudyID  Year    Access1   Access2    Access3
  1        2014       4        4          5
  2        2014       4        5          5
  3        2013       9        4          7
  </code></pre>
  
  <p>This is my Query:</p>
  
  <pre><code>SELECT DISTINCT T.StudyID, T.Year, MIN(T.Access1), MIN(T.Access2), MIN(T.Access3)
  FROM T
  GROUP BY T.StudyID, T.Year
  ORDER BY T.StudyID, T.Year DESC
  </code></pre>
  
  <p>I also tried with this one.</p>
  
  <pre><code> ;WITH MyQuery AS (     SELECT DISTINCT T.StudyID, T.Year, MIN(T.Access1), MIN(T.Access2), MIN(T.Access3),ROW_NUMBER() OVER (PARTITION BY T.StudyID, T.Year ORDER BY T.StudyID, T.Year DESC) AS rownumber
      FROM T  GROUP BY T.StudyID, T.Year      ORDER BY T.StudyID , T.Year DESC ) SELECT * FROM MyQuery WHERE rownumber = 1
  </code></pre>
  
  <p>Any success, I know I am missing something...but dont know what?
  Thanks in advance!!!!</p>