Link to home
Start Free TrialLog in
Avatar of Mr_Shaw
Mr_Shaw

asked on

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()?
Avatar of sihar86
sihar86
Flag of Indonesia image

can you explain why you use self join?
ASKER CERTIFIED SOLUTION
Avatar of Asim Nazir
Asim Nazir
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Shaw
Mr_Shaw

ASKER

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
Avatar of Mr_Shaw

ASKER

I suppose i do

        Select City     ,Checked,      Max(SearchCount)
        from tb1
        group by City,      Checked  
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Shaw

ASKER

What about my approach... will that work

 Select City     ,Checked,      Max(SearchCount)
        from tb1
        group by City,      Checked  
Since you have data and underlying table. So give it a try and if it works then its great.
Avatar of Mr_Shaw

ASKER

Thanks... I was just checking that my approach was not totaly wrong!
Avatar of Mr_Shaw

ASKER

thanks
Avatar of Bhavesh Shah
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
Avatar of Mr_Shaw

ASKER

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'

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