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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:30:44+00:00 2026-05-26T16:30:44+00:00

I have these two tables table1 : date | uid | value_in table2 :

  • 0

I have these two tables

table1 : date | uid | value_in
table2 : date | uid | value_out 

each day, every uid will receive a balance value for both in and out, regardless of the number of transaction/records

what I want to do is to combine the query result to be like this

date | uid | value_in | value_out | (value_in-value_out) as balance

however, when I do this query

SELECT 
  a.date,
  a.uid,
  SUM(a.value_in),
  SUM(b.value_out),
  (SUM(a.value_in)-SUM(b.value_out)) AS balance
FROM table1 a
INNER JOIN table2 b ON a.date=b.date AND a.uid=b.uid
GROUP BY a.date, a.uid

it produce invalid result (the SUM is doubled or tripled)
how should I modify my query so it does not produce doubled result ?

  • 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-26T16:30:44+00:00Added an answer on May 26, 2026 at 4:30 pm

    First build the sum, then join. Like this:

    SELECT i.uid
          ,i.date
          ,i.v_in
          ,COALESCE(o.v_out, 0) AS v_out
          ,(i.v_in - COALESCE(o.v_out, 0)) AS balance
    FROM (
        SELECT date
              ,uid
              ,SUM(value_in) AS v_in
        FROM   table1
        GROUP  BY 1,2
        ) i
    LEFT JOIN (
        SELECT date
              ,uid
              ,SUM(value_out) AS v_out
        FROM   table2
        GROUP  BY 1,2
        ) o USING (date, uid)
    

    The way you had it, every value_in would be combined with every matching value_out, thereby multiplying the numbers. You must aggregate first and then you join one in-sum with one out-sum and everything is groovy. Or is it?

    What happens if there are no value_in or value_out for a given (date, uid)? Or only value_in Or just value_out? Your query will fail.

    I improved by using a LEFT JOIN instead of [INNER] JOIN, but what you really want is a FULL OUTER JOIN – one of the missing features in MySQL.

    You can either provide a list of days in a separate table and LEFT JOIN both tables to it, or you can work around the missing feature with two times LEFT JOIN and UNION. See here for an example.

    Or you could multiply your value_out by -1 UNION both tables together and build one sum.

    But you still would not get a row for a day without any value_in or value_out, which violates your description.

    So, the only clean solution is to have a table with all (date, uid) you want in the result and LEFT JOIN the sums of table1 and table2 to it, or UNION ALL the three (negative table2) in a sub-select and then sum up.

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

Sidebar

Related Questions

I have two tables, table1 ( Id, date, info1) table2 ( Id, date, nvarchar(50)
Ok, So I have these two tables - BioUser- UserId,Weight,DateAdded DimDate-Date // It has
i have these two tables , s(id,firstname,lastname,mydate,mount,sh) users(uid,email,password,nameoffice) and this query $sql=SELECT s.id,s.firstname,s.lastname,s.mydate,s.mount,users.nameOffice FROM
I have two tables table1 and table2. Table2 is having less number of rows
i have these two simple tables: Invoices: ID       Date      ... 1    
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
We have two tables Family and Member, the relation between these two is Family
I have two mysql tables and i want to merge the results of these
I have two tables Plants and Information. For every plant there are many information,
I have two tables which have some transactional stuff stored in them. There will

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.