Solved

MySQL Query - Maximum Frequency

Posted on 2010-08-16
4
609 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

708 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

21 Experts available now in Live!

Get 1:1 Help Now