Solved

MySQL Query - Maximum Frequency

Posted on 2010-08-16
4
613 Views
Last Modified: 2012-08-14
Hi all,

I have a project with a table that contains information of various car models. The data schema is:

Table Name: CarModelTbl
Four fields:
sDate [date]
Model [varchar(50)]
EngineSize [varchar(50)]
Colour [varchar(50)]

The table currently only contains 7 records as displayed below:

2010-07-24, Honda,1.6,Red
2010-07-24,Ford,1.6,Green
2010-07-24,Toyota,1.6,Blue

2010-07-25,Audi,3.2,Red
2010-07-25,BMW,1.8,Green
2010-07-25,Toyota,1.4,Blue
2010-07-25,VW,1.4,Blue

I currently work out the Maximum frequency for a model which occurred the most for July 2010 and this is 2. This is calculated with  the query below:

SELECT Max(A.no_count) FROM (SELECT COUNT(*) as no_count FROM CarModelTbl WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)) as A;

The above query works as expected - But now I have a requirement to also display the model which occurred the most and in this case it would Toyota.

Any ideas how to update the query....

Thanks in advance.

 






 


0
Comment
Question by:mikesung99
  • 2
4 Comments
 
LVL 13

Expert Comment

by:George K.
ID: 33444363
It should be like this
select countmodel.cc, carmodel.model
(
SELECT Max(A.no_count)  cc FROM (SELECT COUNT(*) as no_count FROM CarModelTbl WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)) as A;
) countmodel
left outer join
(
SELECT model, COUNT(*) as no_count FROM CarModelTbl WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)
)   carmodel
on  countmodel.cc=carmodel.no_count
0
 
LVL 13

Accepted Solution

by:
George K. earned 500 total points
ID: 33444367
A correction: 
select countmodel.cc, carmodel.model
from
(

SELECT Max(A.no_count)  cc FROM (SELECT COUNT(*) as no_count FROM CarModelTbl WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)) as A;

) countmodel

left outer join

(

SELECT model, COUNT(*) as no_count FROM CarModelTbl WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)

)   carmodel

on  countmodel.cc=carmodel.no_count

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33444517
SQL Server 2008

;WITH TMP AS (
  SELECT Model, COUNT(*) as no_count
  FROM CarModelTbl
  WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25"
  Group by Model
  HAVING COUNT(*)>0
)
SELECT Model, no_count
from TMP
Where no_count = (select max(no_count) from TMP)
0
 

Author Comment

by:mikesung99
ID: 33444532
Hi georgekl

Thanks the response - I've tested your solution and it worked as expected thank you.

All I did was to remove the closing statement i.e. ';' and it worked illustrated below is the solution iimplemented.

select countmodel.cc, carmodel.model

from
(

SELECT Max(A.no_count)  cc FROM (SELECT COUNT(*) as no_count FROM ptTest.pt_test WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)) as A

) countmodel

left outer join

(

SELECT model, COUNT(*) as no_count FROM ptTest.pt_test WHERE sDATE >= "2010-07-24" AND sDATE <="2010-07-25" Group by Model HAVING ( COUNT('Model ') > 0)

)   carmodel

on  countmodel.cc=carmodel.no_count;


Thanks
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 40
SQL Database Restore 2008 R2 1 27
Troubleshooting Save_Record_Click() VBA 6 21
Append Query Access 2010 4 26
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 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