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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:30:45+00:00 2026-06-04T05:30:45+00:00

I’m doing a pretty complicated query, and I’m slipping at the very last step.

  • 0

I’m doing a pretty complicated query, and I’m slipping at the very last step.

I have managed to get this far:

id  refid           system_id   item_description_id   system_component_id   current_status
711 4fb62cece5313   49          NULL                  711                   RUNNING
712 4fb62cece547d   49          NULL                  712                   STOPPED
713 4fb62cece5616   50          NULL                  713                   RUNNING
714 4fb62cece5803   50          NULL                  714                   STOPPED
716 4fb62cece5ab8   51          NULL                  716                   RUNNING

These are the current statuses of every component of each system. What comes next is to group them by system and count the occurrences that are ‘STOPPED’.

My problem is that some systems will not have any rows with ‘STOPPED’, or any rows at all if the system has never been updated. Yet, I still have to get them as part of the result. Just grouping them by system works for all but these two cases, in which the resulting value is ‘1’ even though there are no stopped components at all.

How can I make a query that will return something like this:

system_id    status_count
49           1
50           1
51           0

And not:

system_id    status_count
49           1
50           1
51           1

With the above table?

  • 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-04T05:30:46+00:00Added an answer on June 4, 2026 at 5:30 am

    You have to use SUM(boolean), like this:

    select system_id, 
      sum(current_status = 'STOPPED') AS stopped_count, 
      sum(current_status = 'RUNNING') as running_count
    from tbl
    group by system_id
    

    Boolean is automatically reduced to integer in MySQL

    Output:

    | SYSTEM_ID | STOPPED_COUNT | RUNNING_COUNT |
    |-----------|---------------|---------------|
    |        49 |             1 |             1 |
    |        50 |             1 |             1 |
    |        51 |             0 |             1 |
    

    Live test: http://www.sqlfiddle.com/#!2/d3bcc/2

    If the COUNT(or any aggregation function for that matter) come from LEFT JOIN, you have to enclose the results with COALESCE or ISNULL:

    select system_id, 
      isnull( sum(current_status = 'STOPPED') , 0 ) AS stopped_count, 
      isnull( sum(current_status = 'RUNNING') , 0 ) as running_count
    from tbl
    group by system_id
    

    ANSI SQL-compliant:

    select system_id, 
      coalesce( sum(current_status = 'STOPPED') , 0 ) AS stopped_count, 
      coalesce( sum(current_status = 'RUNNING') , 0 ) as running_count
    from tbl
    group by system_id
    

    Some database are a little verbose though. Postgresql: http://www.sqlfiddle.com/#!1/d3bcc/2

    select system_id, 
      sum((current_status = 'STOPPED')::int) AS stopped_count, 
      sum((current_status = 'RUNNING')::int) as running_count
    from tbl
    group by system_id
    

    Other database: http://www.sqlfiddle.com/#!3/d3bcc/2

    select system_id, 
      count(case when current_status = 'STOPPED' then 1 end) AS stopped_count, 
      count(case when current_status = 'RUNNING' then 1 end) as running_count
    from tbl
    group by system_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.