0

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.

4

2 回答 2

2
select name,school,max(points) from table group by name,school

这将为您提供每个名称/学校组合的最高分。如果您想要 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

编辑:对不起,这是一个 SQL 解决方案......刚刚看到 MSACCESS 标签。逻辑是正确的,但您需要转换为访问语法。

编辑以更正第二个查询,错过了我的加入中的一列

于 2013-11-14T00:10:13.040 回答
1
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
于 2013-11-14T00:36:31.183 回答