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 7668503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:22:50+00:00 2026-05-31T15:22:50+00:00

If I have a table containing gas stations specifying the city, the name and

  • 0

If I have a table containing gas stations specifying the city, the name and the latitude+longitude of each gas station:

city        | name         | latitude      | longitude
---------------------------------------------------------
Berlin      | GasPump1     | 52.5          | 13.5
Berlin      | GasPump2     | 52.52         | 13.51
Zagreb      | INA1         | 45.8          | 16
Zagreb      | INA2         | 45.81         | 16.01
Zagreb      | INA3         | 45.82         | 15.99

how would I go about obtaining a single representative entity for each city, using a single SQL query? That is, how would I go about implementing the following pseudocode using a single query:

representativeStationsArray = []
citiesArray = sqlQuery("SELECT DISTINCT city FROM gasstations")
for city in citiesArray:
    representativeStation = sqlQuery(
        "SELECT * FROM gasstations WHERE city = ? LIMIT 1;", 
        city)
    representativeStationsArray.append(representativeStation)

I’ve accepted an answer, but I thought it might be interesting to explain my needs for future readers.

On iOS, once the user zooms out, too many annotations being displayed on the map view slows the app down. First experimental solution that I’m trying out (and which should be ok for the country I’m developing this for) is to filter the annotations by city, and just display a random annotation for each city.

If this proves unsatisfactory, I’ll look for another solution.

  • 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-05-31T15:22:51+00:00Added an answer on May 31, 2026 at 3:22 pm

    If by “representative row” you just mean “any arbitrary row where the city matches” then you can just use a GROUP BY:

    SELECT * FROM gasstations
    GROUP BY city
    

    The fields other than city will be populated with data from an arbitrary group-matching row.

    From the docs at http://sqlite.org/lang_select.html#resultset (emphasis mine):

    If the SELECT statement is an aggregate query with a GROUP BY clause …

    Each expression in the result-set is then evaluated once for each
    group of rows. If the expression is an aggregate expression, it is
    evaluated across all rows in the group. Otherwise, it is evaluated
    against a single arbitrarily chosen row from within the group
    . If
    there is more than one non-aggregate expression in the result-set,
    then all such expressions are evaluated for the same row.

    Note: It works this way for SQLite and for MySQL, and a few other databases. But many (most?) databases do not allow you to select non-aggregate fields in a grouped query. So just be mindful of this if you ever switch databases or deploy to a different production database.

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

Sidebar

Related Questions

I have a table containing foodjoint data with their latitude and longitude. I am
I have two table one table containing station id and station name, and another
I have a table containing latitude and longitude values stored as strings ( VARCHAR
I have a table containing a large number of rows. Each row has 2
I have a table containing (essentially) three columns - id, name, ref_id. I would
I have a table containing images and buttons. Each button has a tag with
I have a table containing records of the date and time each product was
Lets say that I have a table containing users . Each row in that
I have a MySQL 5.5 table with a column containing a SET datatype. Each
I have a table containing a large number of rows. Each row has 2

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.