我有一个表格,其信息结构如下:
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
我需要从查询中删除具有相同名称的行,但每个行的最高点都是这样:
ID Points Name School
7 421 James A
2 534 Henry B
4 153 Chris B
有关如何通过查询完成此操作的任何想法?我花了太多时间试图解决这个问题。
答案 0 :(得分: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标记。逻辑是正确的,但您需要转换为访问语法。
编辑以更正第二个查询,错过了我的加入列
答案 1 :(得分: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