Solved

MYSql Question

Posted on 2002-06-05
19
166 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Author Comment

by:AndySulz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Expert Comment

by:Shehzad
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you check Shehzad's last comment.  That seems to be closer to what you want.
0
 
LVL 2

Author Comment

by:AndySulz
Comment Utility
I get the same error.
0
 
LVL 10

Expert Comment

by:arana
Comment Utility
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
Comment Utility
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
Comment Utility
Is it possible to award all three of you some points?
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 100 total points
Comment Utility
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
Comment Utility
i'll see what i can do.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

728 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

10 Experts available now in Live!

Get 1:1 Help Now