[英]Selecting only rows with the highest value of one field, grouped by another field
I have a table that has information structured like this: 我有一个表,其信息结构如下:
ID Points Name School
1 123 James A
2 534 Henry B
3 56 Henry B
4 153 Chris B
5 95 Chris B
6 83 Chris B
7 421 James A
And I need to get out of a query the rows that have the same name, but only the highest points for each like this: 我需要从查询中删除具有相同名称的行,但每个行的最高点才这样:
ID Points Name School
7 421 James A
2 534 Henry B
4 153 Chris B
Any ideas on how this could be accomplished with a query? 关于如何通过查询完成此操作的任何想法? I've spent way too much time trying to figure this out.
我花了太多时间试图解决这个问题。
select name,school,max(points) from table group by name,school
That will give you the max points per name/school combination. 这将为您提供每个名称/学校组合的最高分。 Join it to itself if you want the ID:
如果需要ID,可将其加入自身:
select table.* from table inner join
(select name,school,max(points) as points from table group by name,school) a
on a.name = table.name and a.school = b.school and a.points = table.points
edit : sorry, this is a SQL solution...just saw the MSACCESS tag. 编辑:对不起,这是一个SQL解决方案...只是看到了MSACCESS标记。 Logic is right, but you'll need to convert to access syntax.
逻辑是正确的,但是您需要转换为访问语法。
edit to correct the second query, missed a column inh my join 编辑以更正第二个查询,错过了我加入的列
SELECT
(SELECT TOP 1 ID FROM Table
WHERE
Name = t.Name AND
School=t.School AND
Points=t.Points
) as Id, t.Name, t.Points, t.School
FROM
(SELECT Name, School, max(Points) as Points
FROM Table
GROUP BY Name, School) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.