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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:15:17+00:00 2026-05-20T12:15:17+00:00

Ok, I have a query over two tables. I need to get two sums.

  • 0

Ok, I have a query over two tables. I need to get two sums. I do a group by so the sum() works correctly.

SELECT sum(a.x), sum(b.y) FROM a,b GROUP BY a.n where a.n=b.m

So far this works well, but the problem is i need to group them differently for the second sum (sum(b.y)), than for the first sum (sum(a.x)).

The real query is somewhat more complex but this is my main problem.

This is what i actually try to select sum(stock.amount) - if( sold.amount IS NULL , 0, sum( sold.amount ) )

How can I solve that in one query?

  • 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-20T12:15:18+00:00Added an answer on May 20, 2026 at 12:15 pm

    since you are not writing down the tables I am gonna make a wild guess and assume the tables are like :

    stock : id, item_id, amount
    sold : id, item_id, amount

    then again I assume that you need the stock_in_total, sold_total, left_total counts

    SELECT 
      stock_sums.item_id, 
      stock_sums.st_sum as stock_in_total,
      COALESCE(sold_sums.so_sum,0) as sold_total,
      (stock_sums.st_sum - COALESCE(sold_sums.so_sum,0)) as left_total
    FROM (
      SELECT stock.item_id as item_id, SUM(stock.amount) as st_sum 
      FROM stock 
      GROUP BY item_id
    ) as stock_sums 
    LEFT JOIN (
      SELECT sold.item_id as item_id, SUM(sold.amount) as so_sum 
      FROM sold
      GROUP by item_id
    ) as sold_sums ON stock_sums.item_id = sold_sums.item_id
    

    I hope this would help.

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

Sidebar

Related Questions

I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
I have the following query: WITH cte AS ( SELECT windowId, frameIndx, elemIndx, comment,
I have some doubts over how Enumerators work, and LINQ. Consider these two simple
i have been asked to write a query which in should rank customers base
I have a sql file which has a lot of insert statements (over 3000+).
I have a table where one or more entries with the same 'id' value
I have a task tracking application, which is very basic. It currently has a
I volunteer for a local non-profit that gives out toys and food during the
Here's my table schema [dbo].[Action_History]( [ActionID] [int] IDENTITY(1,1) NOT NULL, [objectID] [int] NOT NULL,
What happened to Dlinq? I heard a few years ago that it was abandoned.

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.