SQL - select the most frequency item
Suppose I have the table as below:
id | name
----------
01 | Tony
02 | Peter
03 | Tony
04 | Tony
05 | John
.. | ..
99 | David
How can I use SQL statement to get the most frequency of the field开发者_Go百科 name(Tony)?
Something along the lines of:
SELECT name
FROM table_name
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 1;
What you are looking for is the Mode. This article explains how to get it (look at the last code example):
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/calculating-mean-median-and-mode-with-sq
SELECT Name, COUNT(*)
FROM YourNames
GROUP BY Name
ORDER BY COUNT(*) DESC
LIMIT 1
精彩评论