Solved

Obtain Value of Max Occurrences

Posted on 2014-02-04
7
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 110

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 110

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 110

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 110

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

756 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