1

I am trying to sum some columns from multiple tables in just one SQL query, but I seem to be getting the wrong results. I think there is a problem with the code that I have provided below. Please any help on this is appreciated.

item Names
 | id | name |
 | 1  |  AB  |
 | 2  |  CA  |

table1
| id  | interest | year |
| 1   |   20.00  | 2014 |
| 2   |   30.00  | 2013 |
| 1   |   10.00  | 2013 |

table2
| id  | deposit  | year |
| 1   |   10.00  | 2014 |
| 2   |   10.00  | 2014 |

This is the query that I tried:

    SELECT 
        a.name,
        b.year,
        sum(b.interest) as 'total'
    FROM
        `table1` b
    INNER JOIN
        `item names` a
    ON
        b.id=a.id
    GROUP BY
        b.id

UNION ALL

    SELECT 
        c.name,
        d.year,
        sum(d.deposit) as 'total'
    FROM
        `table2` d
    INNER JOIN
        `item names` c
    ON
        d.id=c.id
    GROUP BY
        d.id

EXPECTED RESULTS

UPDATE

I am trying to find the total sum of interest and deposit for a particular year and for a particular item

|name   | year  |   total    |
| AB    | 2014  |    30.00   |
| AB    | 2013  |    10.00   |
| CA    | 2013  |    30.00   |
| CA    | 2014  |    10.00   |
3
  • 1
    Could you describe exactly what the expected results should be? It looks like finding the per id, per year totals?
    – ryanyuyu
    Jan 2, 2015 at 17:47
  • You'll need to group by year as well. Jan 2, 2015 at 17:49
  • Exactly what do you expect to come out as the total interest when the data spans across two or more years? You can either show different rows for different years, or one consolidated result for all of them. At this point you're doing neither, since the year column isn't a grouping criteria. MySQL will just show you the first year in the resultset, next to the sum of interests from other years, which is misleading. Also, the syntax isn't b.sum(interest) but sum(b.interest).
    – ffflabs
    Jan 2, 2015 at 17:53

2 Answers 2

2

Perhaps... assuming table1 and table2 have same structure.

First I generate a set with the union values from one and two then we use a simple aggregate and a group by to sum the values by name and year.

SELECT I.Name, B.year, Sum(B.Total) 
FROM item I
INNER JOIN  
(SELECT * FROM table1 UNION select * FROM table2) B
  on B.ID = I.ID
GROUP BY I.Name, B.Year
1
  • I played around with this in SQL Fiddle and it worked. One edit though is that you're missing a FROM in your subquery. I also wrote it a little differently, but it gave the right results. Here's the Fiddle
    – AdamMc331
    Jan 2, 2015 at 19:00
1

In the query you have posted, you need to group by year also to get the results. Then, you can use UNION to get all of the rows from the first set, along with all of the rows from the second set:

SELECT a.name, b.year, SUM(b.interest) AS total
FROM names a
JOIN table1 b ON b.id = a.id
GROUP BY a.name, b.year
UNION
SELECT a.name, c.year, SUM(c.deposit) AS total
FROM names a
JOIN table2 c ON c.id = a.id
GROUP BY a.name, c.year;

However, this doesn't give you your final results, as names that appear in each table ('AB' for example) will appear twice. One row for the year in deposits, one row for the year in interests. To combine those, just use the above as a subquery, sum the totals and again group by name and date:

SELECT name, year, SUM(total) AS total
FROM(
  SELECT a.name, b.year, SUM(b.interest) AS total
  FROM names a
  JOIN table1 b ON b.id = a.id
  GROUP BY a.name, b.year
  UNION
  SELECT a.name, c.year, SUM(c.deposit) AS total
  FROM names a
  JOIN table2 c ON c.id = a.id
  GROUP BY a.name, c.year) temp
GROUP BY name, year;

Here is an SQL Fiddle example.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.