Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL Query - Maximum Frequency

Posted on 2010-08-16
4
Medium Priority
?
618 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
[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
  • 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

597 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