Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SUM on multiple INNER JOIN

I am trying to get the sum of particular fields through multiple table using joins. I am getting the SUM wrong when I I try to get the values together.

I have table State as

| STATE |   MONTH | ID | TOTAL |
--------------------------------
|    FL |    July |  1 | 10000 |
|    CA |   April | 32 |  2000 |

I have another table Balance as

| STATE |  Balance|
-------------------
|    FL |      100|
|    FL |      200|
|    CA |      300|
|    CA |      200|
|    CA |      100|

I have one more table Loan as

| STATE |     Loan|
-------------------
|    FL |      500|
|    FL |      600|
|    CA |      700|
|    CA |      100|
|    CA |      200|

What I want as a result from my query is,

| STATE |     Loan| Balance|
----------------------------
|    FL |     1100|     300|
|    CA |     1000|     600|

When I try to use the following query I am getting the sum for loan with state correctly,

SELECT 
  S.STATE, 
  SUM(L.Loan) 
FROM State AS S
  INNER JOIN Loan AS L ON L.STATE = S.STATE
GROUP BY
 S.STATE

I get the following result,

| STATE |      Loan|
--------------------
|    FL |      1100|
|    CA |      1000|

Similarly I can get the sum from the Balance Table. What I am trying to achieve is to get the sum form both the tables using a single transaction.

Although, I am able to get the desired values, if I do something like this,

SELECT
  STATE AS STATE
  SUM(DataSetB.Balance) AS Balance
FROM
    (
    SELECT 
      STATE AS STATE,
      B.Balance AS Balance
    FROM
        (
        SELECT 
          S.STATE AS STATE, 
          SUM(L.Loan) AS Loan,
        FROM State AS S
          INNER JOIN Loan AS L ON L.STATE = S.STATE
        GROUP BY S.STATE
        )
        AS DataSetL
     INNER JOIN Balance AS B ON B.STATE = DataSetL.STATE
     GROUP BY
       DataSetL.STATE, B.Balance
     ) AS DataSetB
GROUP BY 
  DataSetB.STATE

However this is not very feasible on the large data set that I have. I tried,

SELECT 
  S.STATE AS STATE, 
  SUM(L.Loan) AS Loan,
  SUM(B.Balance) AS Balance
FROM State AS S
  INNER JOIN Loan AS L ON L.STATE = S.STATE
  INNER JOIN Balance AS B ON B.STATE = S.STATE
GROUP BY 
  S.STATE

But this gives me values as multiple of the actual value. It is actually the multiple of number of rows present in the child tables.

I am not asking for an exact solution, but any kind of pointers would be nice.

like image 863
Q2x13 Avatar asked Aug 29 '18 07:08

Q2x13


1 Answers

You can try this, use UNION ALL combine Balance and Loan tables and use a little skill,

  • Balance fill a Loan column set 0
  • Loan fill a Balance column set 0

then JOIN with State do SUM

create table State(
  STATE varchar(50)
);



insert into State values ('FL'); 
insert into State values ('CA'); 
create table Balance(
  STATE varchar(50),
  Balance int
);



insert into Balance values ('FL',100);
insert into Balance values ('FL',200);
insert into Balance values ('CA',300);
insert into Balance values ('CA',200);
insert into Balance values ('CA',100);

create table Loan(
  STATE varchar(50),
  loan int
);

insert into loan values ( 'FL',500);
insert into loan values ( 'FL',600);
insert into loan values ( 'CA',700);
insert into loan values ( 'CA',100);
insert into loan values ( 'CA',200);

Query 1:

SELECT s.STATE,SUM(t1.Loan) as 'Loan',SUM(t1.Balance) as 'Balance'
FROM 
(
  SELECT STATE,0 AS Loan,Balance  
  FROM Balance 
  UNION ALL
  SELECT STATE,Loan,0   
  FROM Loan 
) t1
INNER JOIN State s on s.STATE = t1.STATE
GROUP BY s.STATE

Results:

| STATE | Loan | Balance |
|-------|------|---------|
|    CA | 1000 |     600 |
|    FL | 1100 |     300 |
like image 148
D-Shih Avatar answered Sep 22 '22 06:09

D-Shih