简体   繁体   English

SQL Server Group By Query 选择每个组的第一行

[英]SQL Server Group By Query Select first row each group

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.我想按 StudyID 和 Year 分组,并获取每个字段 Access1 Access2 和 Access3 的最小值,并且只显示最后一年,我的意思是每个组的第一行。 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!!!!提前致谢!!!!

You can GROUP BY StudyID, Year and then in an outer query select the first row from each StudyID, Year group:您可以GROUP BY StudyID, Year然后在外部查询中从每个StudyID, Year组中选择第一行:

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. ROW_NUMBER用于根据Year值为每个StudyID组分配一个订购号。 The row with the maximum Year value is assigned a rn = 1 .具有最大Year值的行被分配rn = 1

Try this:尝试这个:

SELECT DISTINCT T.StudyID, T.Year, MIN(T.Access1), MIN(T.Access2), MIN(T.Access3)
FROM myTable T
WHERE T.Year = (SELECT MAX(T2.Year) FROM myTable T2 WHERE T2.StudyID = T.StudyID)
GROUP BY T.StudyID

Its giving the result you wanted in SQLite, but perhaps in SQL-Server needs some alias I'm not sure.它在 SQLite 中给出了您想要的结果,但也许在 SQL-Server 中需要一些我不确定的别名。 Can't test it right now.目前无法测试。

This is giving the answer you want这是给你想要的答案

SELECT DISTINCT T.StudyID, T.Year, MIN(T.Access1) as Access1, MIN(T.Access2) as Access2, MIN(T.Access3) as Access3
FROM T T
WHERE T.Year = (SELECT MAX(T2.Year) FROM T T2 WHERE StudyID = T.StudyID)
GROUP BY T.StudyID, T.Year
Order by 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM