SQL添加行值并根据此对结果进行排名

时间:2016-11-02 10:44:07

标签: mysql sql

很难解释,我在一个列中有一个包含许多值的表,其中许多值在整个表中重复,目前我只选择这些值出现超过3次的行。

我现在需要做的是查看这些行,在每行中添加单独列的值,然后根据这些行对结果进行排序。

例如,如果天空盒出现的次数超过3次,那么请查看每行的列单击并添加它们,然后根据结果是否比其他行更多或更少来对结果进行排名。

实施例

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

当前查询

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

这可能吗?

2 个答案:

答案 0 :(得分:1)

这是你想要的吗?

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

答案 1 :(得分:0)

试试这个:

select 
  SearchQuery, count(*), group_concat(`clicks`) 
FROM data
GROUP BY SearchQuery
HAVING count(*) > 3
ORDER BY sum(`clicks`) desc