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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:38:23+00:00 2026-06-04T08:38:23+00:00

I have these relations: station (station_id, latitude, longitude) station_control_manager (station_id, ctrl_unit_id) Problem Tabulate station

  • 0

I have these relations:

station (station_id, latitude, longitude)
station_control_manager (station_id, ctrl_unit_id)

Problem

Tabulate station names, ids and total numbers of control areas in the middle town area,
e.g., latitude between 40.750 and 40.760 and longitude between -74.000 and -73.95.

This is my query

SELECT DISTINCT s.station_name, s.station_id, count 
FROM station s, 
   (SELECT cm.station_id, COUNT(cm.station_id) as count
    FROM stationcontrolmanager cm GROUP BY cm.station_id) sub_query
WHERE s.latitude >= 40.750 
  AND s.latitude <=40.760 
  AND s.longitude >= -74.000 
  AND s.longitude <= -73.95 
GROUP BY s.station_id;

I count by grouping station_id and count how many times it repeats (by repating, we know how many control area it has).

Instead, I have this:
enter image description here

The count was supposed to be the number of ctrl_unit_id, which in this case, is also the number of station_id. But in DB I count 7 rows, not 2.

The station names are all correct, of course.

Is there something wrong with my SQL statements?

  • 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-04T08:38:24+00:00Added an answer on June 4, 2026 at 8:38 am

    You can do this without sub-queries:

    SELECT station.station_id, station_name, COUNT(ctrl_unit_id)
    FROM station
    LEFT JOIN station_control_manager ON station.station_id = station_control_manager.station_id
    WHERE (latitude BETWEEN 40.750 AND 40.760) AND (longitude BETWEEN -74.000 AND -73.95)
    GROUP BY station.station_id, station_name
    
    1. Since you need information from both tables, you need to join them using station_id
    2. You need to use LEFT JOIN since a station may have zero control managers (RIGHT JOIN will eliminate station records where there is no corresponding control manager record)
    3. You need to COUNT(ctrl_unit_id) instead of COUNT(*) since the latter will (still) return 1 when a station has no control unit associated with it.
    4. The WHERE clause is self-explanatory
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is Iterator and collections? Does these two have any relations? // the interface
for example suppose we have the two relations R1(A,B) R2(C,D) also these are the
Given from the task, I have these relations: Department (DepCode:pk, DepName, DepAdress) LoanAccs (LoanAcc:pk,
I have to tables:users and messages.And create the one-many relations between these tables.And,If I
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I
I have these ER relations: Person ID_PERSON NAME Animal ID_ANIMAL NAME Person_animal ID_PERSON ID_ANIMAL
We are using builder pattern to generate test data. These domain objects have relations
We have two tables Family and Member, the relation between these two is Family
I have these 3 icons enclosed in separate DIVs all of which are enclosed
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;

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.