Lewis Smith Lewis Smith - 9 months ago 80
MySQL Question

SQL Add row values and rank results based on this

Hard one to explain, I have a table with many values in one column where many repeat throughout the table, at the moment I am only chosing the rows where these values appear more than 3 times.

What I now need to do is look at these rows add the values of a seperate column in each row and then order the results based on these.

Forexample, if skybox appears more than 3 times, then look at the column clicks on each row and add them, then rank that result based on if it has more or less than the others.


Skybox appears 4 times and has Clicks values 12,13,1,19
test appears 3 times and has Clicks values 23,42,1,43
Test should rank higher in the results as it has more clicks when added

Current query

SELECT * FROM `data` GROUP BY SearchQuery HAVING COUNT(*) >= 3

Is this possible?

Answer Source

Is this what you want?

select name, sum(clicks)
from t
group by name
having count(*) >= 3
order by sum(clicks) desc;