这是我在sql-server中的表结构:
id Marks
---------- -----------
AAA 50
KKK 87
KKK 89
BBB 48
CCC 54
AAA 52
DDD 55
BBB 60
XXX 99
这是所需的输出:
Name attempts Max Mark
------- ---------------- ------------
AAA 2 52
kkk 2 89
BBB 2 60
CCC 1 54
DDD 1 55
XXX 1 99
我试过这个,但似乎不对:
SELECT
name,
count(*) as attempts,
max(marks)
FROM table_name
GROUP BY name, attempts, max_marks
答案 0 :(得分:4)
试试这个:
SELECT
id AS Name,
count(id) AS attempts,
max(Marks) AS Max_Mark
FROM table_name
GROUP BY id
答案 1 :(得分:0)
你在正确的轨道上添加了太多的分组字段,
SELECT id AS name
,COUNT(*) AS Attempts
,MAX(marks) as MaxMarks
FROM table_name
GROUP BY id
如果您的列已经是MAX
或COUNT
等汇总函数的一部分,则不需要将其包含在GROUP BY
子句中