简体   繁体   中英

SQL: Count the values of a column per record

I am stuck on creating a query.

I got a table "videos" with this structure:

id | artist | song | hits

records are like:

1 | Rihanna | Song1 | 400
2 | Rihanna | Song2 | 100
3 | Prince | Song45 | 300
4 | The Police | Song456 | 1000

This is my non-working query:

SELECT DISTINCT artist, SUM(hits)
FROM videos
ORDER BY hits DESC

The result is:

Rihanna | 1800

But I'd like a result like this:

Rihanna | 500
Prince | 300
The Police | 1000

What am I doing wrong here?

SELECT artist, SUM(hits) as hitscount
FROM videos
GROUP BY artist
ORDER BY hitscount DESC

added suggestions from the comments. thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM