Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:30:54+00:00 2026-06-06T00:30:54+00:00

I am trying to write a query and would like some help if possible.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:30:56+00:00Added an answer on June 6, 2026 at 12:30 am

    How about

    select id, name, city, state, selcount from t 
    where exists
    (
      select 1 from
      (select name, city, state, max(selcount) selcount
         from t 
        group by name, city, state) s
      where s.name = t.name and s.city = t.city and s.state = t.state and s.selcount = t.selcount
    )
    

    I’ve built a SQL Fiddle for this to show a working example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a query to create a 'table' of data as
I'm trying to write a query that extracts and transforms data from a table
I'm trying to write some LINQ To SQL code that would generate SQL like
I am trying to write some vba code in Excel to query a table
I'm trying to write an SQL query that would search within a CSV (or
I'm trying to write a query I don't want to have Cartesian products on.
I am trying to write a query that connects 3 tables. The first table
I'm trying to write a query to tell me which orders have valid promocodes.
I am trying to write a query that joins another table, but in this
I am trying to dynamically write some xml for a webservice. I have recently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.