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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:40:21+00:00 2026-06-07T02:40:21+00:00

I am wondering why the below query is not retrieving the difference between two

  • 0

I am wondering why the below query is not retrieving the difference between two aggregate functions(sum) properly ?

SELECT 
    epa.status as Status,
    sum(equip_port_usage.total_nbr_ports) as Total,
    sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Used,
    sum(equip_port_usage.NBR_PORTS_ASSIGNED)
      - sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Difference
  FROM site_inst
  INNER JOIN site_attr_settings
     ON site_attr_settings.site_inst_id = site_inst.site_inst_id
  INNER JOIN epa on epa.site_inst_id=site_inst.site_inst_id
  INNER JOIN equip_inst ON equip_inst.site_inst_id=site_inst.site_inst_id
  INNER JOIN equip_port_usage
     ON equip_port_usage.equip_inst_id=equip_inst.equip_inst_id
WHERE site_inst.SITE_HUM_ID = 'CLEUS'
GROUP BY epa.status;

The output is like below

Ok        303876  10276     0
Faulty     19044    644     0
Reserved   19872    672     0

I am expecting it to be

Ok        303876  10276  293600
Faulty     19044    644   18400
Reserved   19872    672   19200
  • 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-07T02:40:23+00:00Added an answer on June 7, 2026 at 2:40 am

    It’s a copy and paste failure in your code.
    You’re calculating

    sum(equip_port_usage.NBR_PORTS_ASSIGNED)
     - sum(equip_port_usage.NBR_PORTS_ASSIGNED)
    

    which is always 0.

    Try:

    SELECT 
        epa.status as Status,
        sum(equip_port_usage.total_nbr_ports) as Total,
        sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Used,
        sum(equip_port_usage.total_nbr_ports)
         - sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Difference
      FROM site_inst
      INNER JOIN site_attr_settings
         ON site_attr_settings.site_inst_id = site_inst.site_inst_id
      INNER JOIN epa ON epa.site_inst_id=site_inst.site_inst_id
      INNER JOIN equip_inst ON equip_inst.site_inst_id=site_inst.site_inst_id
      INNER JOIN equip_port_usage
         ON equip_port_usage.equip_inst_id=equip_inst.equip_inst_id
     WHERE site_inst.SITE_HUM_ID = 'CLEUS'
     GROUP BY epa.status
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering the following, the query below will select the appropriate fields if a
I'm wondering about the feasability of this below. Of course, I'm not asking for
I was wondering, whats the best practice on the example below. <?php if(isset($_POST['query'])){ $out
I have a query below that works but was wondering if there is a
I was wondering if the below scenario will work? I am having trouble with
Just wondering about this code below... when I turn off my internet connection and
I was wondering if the codes below are the correct way to check for
Please see the jQuery below: I am really wondering how can I use CSS
The getCategory method below seems very redundant and I was wondering if anyone has
I am wondering if there is a way to simplify the nested loop below.

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.