I have been asked to return the most popular record from a group of results.

I have been asked to return the most popular record from a group of results.

Do i use a self join with MAX()?
Mr_ShawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sihar86Commented:
can you explain why you use self join?
0
Asim NazirCommented:
You need to have a field showing number of hits to a record. You can simply get Max(HitField) to achieve what you are looking for. Is it?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_ShawAuthor Commented:
I am not doing anything yet.

I have three columns, with data. I need to return the highest SearchCount for each city?

City      Checked      SearchCount
Brighton      r      10
Brighton      r      20
Brighton      a      10
London      a      50
London      a      60
London      a      70
London      b      80
London      g      90
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Mr_ShawAuthor Commented:
I suppose i do

        Select City     ,Checked,      Max(SearchCount)
        from tb1
        group by City,      Checked  
0
Asim NazirCommented:
If you need to show all the records with Highest count first then simply OrderBy SearchCount Desc.
Else
You can get single record by adding a where clause like:
Select * from A where SearchCount = (Select Max(SearchCount) from A)

Something like above should get you back on track.
0
Mr_ShawAuthor Commented:
What about my approach... will that work

 Select City     ,Checked,      Max(SearchCount)
        from tb1
        group by City,      Checked  
0
Asim NazirCommented:
Since you have data and underlying table. So give it a try and if it works then its great.
0
Mr_ShawAuthor Commented:
Thanks... I was just checking that my approach was not totaly wrong!
0
Mr_ShawAuthor Commented:
thanks
0
Bhavesh ShahLead AnalysistCommented:
Hi Author,

Its depend, what do you wanted.
Your approach work but Searchcount will take maximum per city & checked.
And will return ONE ROW Per City & Checked

Below query will be fetch data with maximum searchcount irrespective of city & checked.
Select * from A where SearchCount = (Select Max(SearchCount) from A)
And will return only ONE ROW
0
Mr_ShawAuthor Commented:
I used the following SQL.

Select
ID,
City,
Checked,
SearchCount
from A where SearchCount = (Select Max(SearchCount) from A where city = 'London')
and city = 'London'

0
Bhavesh ShahLead AnalysistCommented:
HI,

You can use this too(you must be knowing still)

Select
ID,
City,
Checked,
SearchCount
from A, (select city,Max(SearchCount)SearchCount from A) B
where A.City = B.City
And A.SearchCount = B.SearchCount
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.