I am trying to write a query and would like some help if possible. Thanks in advance.
I have a table of facility data (~100k rows) that I am getting from a public source. That data contains several records for what I would consider to be the same place (same name, city, state), they just have different suite numbers. The other interesting bit of code is that I have a selection counter on the data that I increment anytime someone chooses one of the facilities. This way, I can use the selection count along with some other weight calculations to make results show higher in a list.
What I am trying to do is write a query that when someone enters a search query, it will show only one record for the facility, the one with the highest selection count, and omit the rest.
Note: I do not want to do any preprocessing to the data as it is going to get re-loaded monthly.
Scheama:
ID
Name
Address 1
Address 2
City
State
Zip
Phone
Selection Count
Example Search: “women”
ID Name City State Selection Count
1 Brigham & Women's Hospital Boston MA 22
2 Brigham & Women's Hospital Cambridge MA 0
3 Brigham & Women's Hospital Boston MA 5
4 Brigham & Women's Hospital Boston MA 1
5 Brigham & Women's Hospital Orlando FL 3
6 Woman's Hospital of Detroit Detroit MI 100
7 Brigham & Women's Hospital Boston MA 0
8 Woman's Hospital of Detroit Detroit MI 55
What I’d like is a resultset that contains 1, 2, 5, 6
1,3,4,7 Are the same so bring back the top selection count. Same for 6 and 8.
I am sure that there is a having and a top clause in here somewhere, but I have not been able to get this to do what I want.
Thoughts?
How about
I’ve built a SQL Fiddle for this to show a working example.