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?
Asim NazirProject ManagerCommented:
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?

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
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Mr_ShawAuthor Commented:
I suppose i do

        Select City     ,Checked,      Max(SearchCount)
        from tb1
        group by City,      Checked  
Asim NazirProject ManagerCommented:
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.
Mr_ShawAuthor Commented:
What about my approach... will that work

 Select City     ,Checked,      Max(SearchCount)
        from tb1
        group by City,      Checked  
Asim NazirProject ManagerCommented:
Since you have data and underlying table. So give it a try and if it works then its great.
Mr_ShawAuthor Commented:
Thanks... I was just checking that my approach was not totaly wrong!
Mr_ShawAuthor Commented:
thanks
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
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'

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
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.