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

  • Home
  • SEARCH
  • 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 9237295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:30:18+00:00 2026-06-18T07:30:18+00:00

I have a query that is returning systems and areas from my database like

  • 0

I have a query that is returning systems and areas from my database like so:

SELECT  Areas.ID AreaID,
    Areas.Name AreaName,
              Systems.*

FROM Systems
INNER JOIN Areas ON Areas.ID = Systems.AreaID
WHERE ....

THis returns data that looks like the following:

|  AreaID   |  AreaName   | SystemName  | ...
|     1     |    area1    |     sys1    |
|     1     |    area1    |     sys2    |
|     1     |    area1    |     sys3    |
|     1     |    area1    |     sys4    |
|     2     |    area2    |     sys5    |
|     2     |    area2    |     sys6    |

I would like to return an additonal column containing the number of systems in each area returned, so that I end up with something like this:

|  AreaID   |  AreaName   | SystemName  |  noOfSystems  | ...
|     1     |    area1    |     sys1    |       4       |
|     1     |    area1    |     sys2    |       4       |
|     1     |    area1    |     sys3    |       4       |
|     1     |    area1    |     sys4    |       4       |
|     2     |    area2    |     sys5    |       2       |
|     2     |    area2    |     sys6    |       2       |

I.E. There are 4 systems with the area id of 1 and 2 with the area id of 2.

How can this be done? I’m sure i’ve heard of a built in function that does this but I can’t find quite what I want.

  • 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-18T07:30:19+00:00Added an answer on June 18, 2026 at 7:30 am

    You will want use the aggregate function COUNT() and then GROUP BY. This can be done in a correlated subquery:

    SELECT  Areas.AreaID AreaID,
        Areas.areaname AreaName,
        s1.SystemName,
        (select count(systemname) 
         from systems s2
         where s1.areaid = s2.areaid
         group by areaid) noOfSystems
    FROM Systems s1
    INNER JOIN Areas 
        ON Areas.AreaID = s1.AreaID
    WHERE ....
    

    See SQL Fiddle with Demo

    Or you can use a subquery that you join to get the total count:

    SELECT  Areas.ID AreaID,
        Areas.Name AreaName,
        s1.SystemName,
        s2.NoOfSystems
    FROM Systems s1
    INNER JOIN Areas 
        ON Areas.ID = s1.AreaID
    INNER JOIN
    (
        select COUNT(SystemName) NoOfSystems,
            AreaID
        from systems
        group by AreaID
    ) s2
        on s1.AreaID= s2.AreaID
    WHERE ....
    

    See SQL Fiddle with Demo

    This version uses a subquery to get the total count and then you join that back to the Systems table to return additional columns, if needed.

    Or, you can use windowing functions if your RDBMS has this option by using Count() over():

    SELECT  Areas.ID AreaID,
        Areas.Name AreaName,
        Systems.SystemName,
        COUNT(SystemName) over(partition by Areas.ID, Areas.AreaName) as NoOfSystems
    FROM Systems
    INNER JOIN Areas 
        ON Areas.ID = Systems.AreaID
    WHERE ....
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have a query that looks like thos : SELECT max(insert_date), creative_id, creative_object FROM
I have a query that creates a result set like this: Rank Name 1
I have a query that looks like this: select id , int1 , int2
I have a query that is returning in vastly different amounts of time between
I have a simple Nhibernate Linq query that is returning more results than expected:
So I have this awesome MySQL query that's returning me an awesome array >
This query that I have is returning therapists whose 'therapistTable.activated' is equal to false
I have the following LINQ expression that's not returning the appropriate response var query
I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them

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.