I'm trying to write a query which will get the maximum scores for the most popular songs, in order of song popularity.
I have a table called "gameplay" with the following fields:
id
song_id
score
(plus some other arbitrary fields)
So far, the closest I've got is this:
SELECT id, song_id, score
FROM (
SELECT id, song_id, score
FROM (
SELECT id, song_id, score
FROM gameplay
ORDER BY score DESC
) AS all_scores_in_order
GROUP BY song_id
) AS top_scores_per_song
ORDER BY FIELD (song_id, 3,1,2)
But I would like the values in ORDER BY FIELD to be generated by another subquery - the song_id ranked by popularity (in order of counting row occurrences in table) ie.
SELECT song_id
FROM gameplay
GROUP BY song_id
ORDER BY count( id ) DESC
Can't you just group and then order by Count(id) in the outer query?
This should show all sorts sorted by the number of occurrences, and showing the max score:
SELECT song_id, COUNT(id), MAX(score)
FROM gameplay
GROUP BY song_id
ORDER count(id) DESC
Or do you want each song_id to appear several times?
Related
I have table in MySQL DB which contains among other things two fields user_id and score. This table is kind of log table so there can be multiple rows for one user_id with different scores. How can I get only top 10 users with highest score from this table?
SELECT DISTINCT user_id
FROM your_table
ORDER BY score DESC
LIMIT 10
EDIT:
SELECT DISTINCT *
FROM your_table
WHERE (user_id, score) IN (SELECT user_id, MAX(score) AS score
FROM your_table
GROUP BY user_id)
ORDER BY score DESC
LIMIT 10
SqlFiddleDemo
This is basic and you should put more effort; here is atemplate you can use -
SELECT TOP 10 distinct *
FROM people
WHERE names='SMITH'
ORDER BY names asc
I am trying to create a ranking system using the data in my table. It has 2 columns, name and score. One user can have multiple scores. This is what I have so far:
("SELECT name, score, FIND_IN_SET( score, (SELECT GROUP_CONCAT ( score ORDER BY score DESC ) FROM data )) AS rank FROM scores");
But this command lists all the user's previous scores when I only need the highest score.
Does anyone know how I can achieve this?
Try
SELECT name, sum(score) from scores group by name order by sum(score) desc limit 1
And if you don't want to sum then use
SELECT name, max(score) from scores
The below query will pop the list of name in the order of highest score respectively.
select name, max(score) as score from scores group by name order by score desc
You can use SELECT DISTINCT name, score ...
and Select TOP 1 and ORDER BY
This is my first question here so I hope im doing it right. I have the following SQL query:
SELECT *,
MATCH(name, descr) AGAINST ('$q') AS score
FROM songs
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score DESC
As you may know, this query selects everything from every row from the songs table, if there is a match in the description or in the name .What I want to do,is to limit the query only to search the latest 10k rows for example. I also have a primary key, id.
You will need a nested query. Try this:
SELECT temp.*,
MATCH(name, descr) AGAINST ('$q') AS score
FROM (
SELECT *
FROM songs
ORDER BY id DESC
LIMIT 10000
) temp
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score DESC
You can ORDER BY score ASC (instead of DESC) and add LIMIT 10000 and finally reverse the order of the result :
SELECT * FROM (
SELECT *,
MATCH(name, descr) AGAINST ('$q') AS score
FROM songs
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score ASC
LIMIT 10000
) ORDER BY score DESC
i have a currently listening box on the homepage of my music website, and i have a table like this:
id,userid,songid,time
now every time a user plays a song, the id of the song is inserted into the songid field and also the time. so sometimes i have duplicate songid (inserted by different users).
Now here is my sql statement:
SELECT DISTINCT songid,time FROM songs ORDER BY time DESC LIMIT 10
that gives me the last ten songs that were listened, however sometimes it returns duplicate data too! how can i fix this problem? Thanks in advance
You'll want to aggregate over songid:
SELECT songid, MAX(time) AS most_recent_time
FROM songs
GROUP BY songid
ORDER BY most_recent_time DESC
LIMIT 10
If the time is the datetime the song was last played, you can group the gons with the last time each was played:
SELECT songid,MAX(time)
FROM songs
GROUP BY songid
ORDER BY time DESC
LIMIT 10
SELECT *
FROM songs s
WHERE id =
(
SELECT id
FROM songs si
WHERE si.songid = s.songid
ORDER BY
si.songid DESC, si.time DESC, si.id DESC
LIMIT 1
)
ORDER BY
time DESC, id DESC
LIMIT 10
Create the following indexes:
songs (songid, time, id)
songs (time, id)
for this to work fast.
Unlike the GROUP BY songid solutions, this does not require a full table scan or a full index loose scan on songs.
I have a table with feilds : file_id, rating, user_id
There is one rating per user_id, so there could be many rating (in scale of 0-5) for a single file_id.
I want to find avg of ratings for every file_id and then display 5 file_id with highest avg rating.
Actually my sql query looks like:
SELECT m.server_domain, m.original_name, m.type, m.title, m.views,
m.description, m.hash, AVG(mr.rating_scale5) as avg_rating_scale5
FROM c7_media m, c7_storage s, c7_media_ratings mr
WHERE s.public=1 AND m.storage_hash = s.hash AND m.hash = mr.media_hash
GROUP BY mr.media_hash
How should I do this?
Zeeshan
Group by a file_id and then simply order by the average. Cut off all records that fall below the top 5.
SELECT
file_id, AVG(rating) as avg_rating
FROM
table
GROUP BY
file_id
ORDER BY
avg_rating DESC
LIMIT 5
SELECT `file_id`, AVG(`rating`) as a FROM `table`
GROUP BY `file_id` ORDER BY a DESC LIMIT 5
Replace 'table' with the name of your table.