2011-09-26 61 views
0

我有一个包含字段的数据表:search, search_location, num_searches。我想放在一起SELECT语句,将产生100个最流行的search_locations的列表,通过为所有搜索的num_searchesSUM()确定与同search_location(不管search字段的值)。我怎样才能做到这一点?使用SQL检索数据集中最流行的查询

回答

6

您可以使用GROUP BY,一种通过将共享某些相同值的所有行分组来减少表的方法。

SELECT search_location, SUM(num_searches) as total_searches 
    FROM my_table 
    GROUP BY search_location 
    ORDER BY total_searches DESC 
    LIMIT 100;