我有一个像这个id,用户名,分数的表格T. 而我想要做的是为每个用户选择他的用户名和他的积分总和并按点排序。 我试过这个,但它没有用:
SELECT username, sum(points) as s
FROM T
group by username,points
order by points desc
结果:
username points
3 2
3 3
3 0
4 0
6 0
7 0
3 -1
答案 0 :(得分:3)
如果您要汇总积分,则不希望在GROUP BY语句中包含积分。此外,如果您将点数总和重命名为s,则可以在ORDER BY中引用s
SELECT username, sum(points) as s
FROM T
group by username
order by s desc
答案 1 :(得分:0)
删除积分的原因是因为您按点分组,每个单独的值分组在:
SELECT username, sum(points) as s
FROM T
group by username
order by sum(points) desc
然后订购Sum(积分)