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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:14:33+00:00 2026-05-12T01:14:33+00:00

In a follow-up to a previous question, let’s say I have 3 tables, A,

  • 0

In a follow-up to a previous question, let’s say I have 3 tables, A, B, and C. Table A has an ID which is used as a foreign key on tables B and C, each of which has some value as an attribute. For each ID in table A, I want to get the difference in value between tables B and C, which can be done as follows:

CREATE VIEW T1 AS
    SELECT B.A_ID AS ID, SUM(B.VALUE) AS VAL
    FROM B
    GROUP BY B.A_ID;
CREATE VIEW T2 AS
    SELECT C.A_ID AS ID, SUM(C.VALUE) AS VAL
    FROM C
    GROUP BY C.A_ID;
SELECT T1.ID, T1.VAL, T2.VAL FROM T1, T2 WHERE T1.ID = T2.ID;

The problem is, what if table B has some values for a particular ID, but table C does not, or vice versa. In that case, my select statement will not return that row. Is there a way for me to create a single view, which essentially looks like the following:

CREATE VIEW T3 AS
    SELECT B.A_ID AS ID, SUM(B.VALUE) AS VAL1, SUB(C.VAL) AS VAL2
    FROM B, C
    WHERE B.A_ID = C.A_ID
    GROUP BY B.A_ID;

An example of the creation script for such a view would be appreciated.

  • 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-12T01:14:33+00:00Added an answer on May 12, 2026 at 1:14 am

    You can use this

    CREATE VIEW myView AS
    SELECT test_a.id, name, IFNULL( (
      SELECT SUM( value ) 
      FROM test_b
      WHERE test_b.a_id = test_a.id
      GROUP BY test_b.a_id ) , 0
    ) - IFNULL( (
      SELECT SUM( value ) 
      FROM test_c
      WHERE test_c.a_id = test_a.id
      GROUP BY test_c.a_id ) , 0
    )
    FROM test_a
    

    But this will result in negative numbers if c has a higher sum than b. If you want the absolute difference than use this:

    CREATE VIEW myView AS
    SELECT test_a.id, name, ABS( IFNULL( (
      SELECT SUM( value ) 
      FROM test_b
      WHERE test_b.a_id = test_a.id
      GROUP BY test_b.a_id ) , 0 ) - IFNULL( (
    SELECT SUM( value ) 
      FROM test_c
      WHERE test_c.a_id = test_a.id
      GROUP BY test_c.a_id ) , 0
      )
    )
    FROM test_a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up to a previous question which seems to have confused
A follow-up to my previous question. I have a button on my stage, which
This is a follow-up question to my previous post: Python/Scipy Interpolation (map_coordinates) Let's say
This is a follow up from my previous question I have this code basically
This is a follow-up to my previous question about developing Perl applications . Let’s
This question is a follow up on a previous asked question: link I have
Follow up question to a previous question , this has been identified as a
This is a follow-on from a previous question, in the implementation, I have two
this is a follow up question of my previous question So I have this
As a follow up on my previous question here: Link These are my tables:

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.