SQL Server Group By Query Select first row each group

user2112420

I am trying to do this query. This is what I have.

My table is: Table

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

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.

StudyID  Year    Access1   Access2    Access3
1        2014       4        4          5
2        2014       4        5          5
3        2013       9        4          7

This is my Query:

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

I also tried with this one.

 ;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

Any success, I know I am missing something...but dont know what? Thanks in advance!!!!

Giorgos Betsos

You can GROUP BY StudyID, Year and then in an outer query select the first row from each StudyID, Year group:

SELECT StudyID, Year, minAccess1, minAccess2, minAccess3
FROM (
   SELECT StudyID, Year, min(Access1) minAccess1, min(Access2) minAccess2,
          min(Access3) minAccess3, 
          ROW_NUMBER() OVER (PARTITION BY StudyID ORDER BY Year DESC) AS rn 
   FROM mytable
   GROUP BY StudyID, Year ) t
WHERE t.rn = 1

ROW_NUMBER is used to assign an ordering number to each StudyID group according to Year values. The row with the maximum Year value is assigned a rn = 1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Select first row in each GROUP BY group?

From Dev

Select first row in each GROUP BY group

From Dev

Select first row from group each by with count using Big Query

From Dev

Select first row from group each by with count using Big Query

From Dev

SQL Query to select each row with max value per group

From Java

How to select the first row of each group?

From Dev

Laravel eloquent select first row of each group by

From Dev

Select first row in each contiguous run by group

From Dev

Select first row in each contiguous run by group

From Dev

pandas: how do I select first row in each GROUP BY group?

From Dev

Select first row in each group using group by in sqlite

From Dev

First row for each group

From Java

SQL Server Add row number each group

From Dev

Explain the query to select the first two records of each group by a “SELECT” command

From Dev

get first row for each group

From Dev

SQL query select and group by

From Dev

SQL query select and group by

From Dev

Count and select first row by group

From Dev

How To get the First Row Form SQL Group Query?

From Dev

SQL return first row of a group

From Dev

Show only first row within a group for Microsoft SQL Server

From Dev

SQL Server: Select Max for each sub group in result set

From Dev

SQL Server 2005 GROUP BY and COUNT query for each month

From Dev

SQL Server 2005 GROUP BY and COUNT query for each month

From Dev

SQL Server: group by a column and get the first group

From Dev

SQL Server Group by each Day?

From Java

Pandas dataframe get first row of each group

From Dev

Adding a rank to first row of each group

From Dev

KDB selecting first row from each group

Related Related

HotTag

Archive