SQL:计算每个记录的列值
问题描述:
我一直在创建查询。SQL:计算每个记录的列值
我有一个表 “视频” 这一结构:
id | artist | song | hits
记录是这样的:
1 | Rihanna | Song1 | 400
2 | Rihanna | Song2 | 100
3 | Prince | Song45 | 300
4 | The Police | Song456 | 1000
这是我的非工作查询:
SELECT DISTINCT artist, SUM(hits)
FROM videos
ORDER BY hits DESC
结果是:
Rihanna | 1800
,但我想这样的结果:
Rihanna | 500
Prince | 300
The Police | 1000
我在做什么错在这里?
答
SELECT artist, SUM(hits) as hitscount
FROM videos
GROUP BY artist
ORDER BY hitscount DESC
在评论中添加了建议。谢谢!
添加'GROUP BY艺术家'(在ORDER BY之前)无需做DISTINCT。 – jarlh