Solved

MYSql Question

Posted on 2002-06-05
19
183 Views
Last Modified: 2010-05-02
I know how to find the max value of a Mysql column, but is there a function that will give you the max number of occurences that let say the number 1 occurs in a particular column?

Thanks
0
Comment
Question by:AndySulz
  • 9
  • 5
  • 4
  • +1
19 Comments
 

Expert Comment

by:Shehzad
ID: 7057768
hi,

U can use where clause
as in sql :
select count(column_name) from table_name
where column_name like '1';



0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7057794
or if "1" is part of a string:

Select Count(column_name) as FieldCount
  from tblWhatever
 where column_name like '%1%'

In VB, you can then retrieve this from recordset field rstXXX.field("FieldCount").Value
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7057827
Sorry, I actually wrote the question wrong...  let say my column in the db looks like this...
1
2
1
1
3
2
1
Is there a build in function in sql that will say the maximum number of occurences is 1?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 2

Author Comment

by:AndySulz
ID: 7057835
also, is the count(column_name) faster than rs.movelast ... rs.recordcount?  I did increase the points because now it turned in to two questions.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7057874
Actually, I think Shehzad's comment answers your question.  And I think that the count will work without the WHERE clause, but movelast/recordcount will not work unless the WHERE clause is included, in which case the speed is probably a tiny bit faster.
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7057899
With out the WHERE clause I think it will count how many rows of data are in the column...i want it to say that the number 1 occurs the most in my sample dataset....basically like the Mode of the data set.
0
 

Expert Comment

by:Shehzad
ID: 7057918
for this purpose u can include the max(fieldname) instead of
count(fieldname)
i.e.

select max(field_name) from table_name;

---
now if you table data is like this
----
a
a
a
b
b
c
a
b
c

---
then the result will be :
a
0
 

Expert Comment

by:Shehzad
ID: 7057920
for this purpose u can include the max(fieldname) instead of
count(fieldname)
i.e.

select max(field_name) from table_name;

---
now if you table data is like this
----
a
a
a
b
b
c
a
b
c

---
then the result will be :
a

:rsphitz
--------

if u remove the where clause the result will be the total number of rows in the table
where first we required on '1''s count

0
 
LVL 2

Author Comment

by:AndySulz
ID: 7057942
Shehzad's solution counts how many 1's occur...and with out the Where clause it will count the total number of rows in the column.  your solution will find the maximum value, in which I shall run on my sample dataset, I will get back the number 3 since 3 > 1 and 3 > 2 ... I do not want the maximum value, I do want the max number of occurences.  i.e. the mode in math terms.
0
 

Expert Comment

by:Shehzad
ID: 7057962
sorry missed this out

try this one, this will give the maximum occurence on top of the list :
********
select count(field_name), field_name from table_name
group by field_name
order by count(field_name) desc
********
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7057997
I seem to recall someone once doing this by doing a count combined with a DISTINCT command, but I don't remember the exact code.  Does this work or can anyone figure it out?

If may be something like this:

select max(
   count(
      select DISTINCT field_name FROM tblxxx
   )
) from tblxxx
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7058025
i tried...
select distinct count(field), field from table group by field order by count(field) desc limit 1

but I get an Invalid use of group function  I am using the windows version of mysql keep in mind...
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7058031
Did you check Shehzad's last comment.  That seems to be closer to what you want.
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7058036
I get the same error.
0
 
LVL 10

Expert Comment

by:arana
ID: 7058045
remove the order by just leave the group by and tell us the results. (also remove the distinct)
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7058056
I got it guys...

Select count(field) as tmp, field from table group by field order by tmp desc limit 1;
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7058057
Is it possible to award all three of you some points?
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 100 total points
ID: 7059755
To split points, add a zero-point question in the customer service topic area with a reference to this Q and describe how you'd like the points split.  Glad you got it!
0
 
LVL 2

Author Comment

by:AndySulz
ID: 7059862
i'll see what i can do.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 95
MS Access 03, TransferText, decimal places 8 69
IF ELSE Statement in Excel Macro VBA 16 64
Error with a code discussed on this page 5 14
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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