Solved

Obtain Value of Max Occurrences

Posted on 2014-02-04
7
379 Views
Last Modified: 2014-02-04
Hello Experts!

Who can tell me what the most efficient way of obtaining the value of a column that occurs most often within that column?  I need something that does not require much memory or CPU.

In other words, let's say that I have a column in a table with the following values:

1
2
3
2
3
3
1
3
3
1
3
2
3

I need a mysql statement that will give me the answer of "3" for this column as the value that occurs the most times in that column.

Thank you.
0
Comment
Question by:OmniUnlimited
7 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39833059
$sql = "SELECT fname, COUNT(fname) AS num FROM my_table GROUP BY fname ORDER BY num DESC";
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39833063
The ouputs will look something like this:
USING MySQLi_Result::Fetch_Object():
stdClass Object
(
    [fname] => Ray
    [num] => 2
)

stdClass Object
(
    [fname] => Bill
    [num] => 1
)

stdClass Object
(
    [fname] => John
    [num] => 1
)
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39833067
SELECT colname, COUNT(*) as total
FROM tablename
GROUP BY colname;

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39833087
@Jagadishwor Dulal: Have you tested that query?

@OmniUnlimited: I recommend that you test any suggestions before you accept answers!
0
 
LVL 17

Author Closing Comment

by:OmniUnlimited
ID: 39833097
Ray thank you so much!  As always, your genius just shines.

I tested the queries and only yours gave me the results I was looking for.

Have a great day!
0
 
LVL 32

Expert Comment

by:awking00
ID: 39833124
select columnname from
(select columnname, count(*) from table group by columnname order by count(*) desc)
limit 1;
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39833647
Thanks for using EE and thanks for the points and your kind words!  Best regards, ~Ray
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now