a simple way to sum a result from UNION in MySql
Solution 1
select id, sum(amount) from (
select id,amount from table_1 union all
select id,amount from table_2 union all
select id,amount from table_3
) x group by id
Solution 2
SELECT id, SUM(amount) FROM
(
SELECT id, SUM(amount) AS `amount` FROM t1 GROUP BY id
UNION ALL
SELECT id, SUM(amount) AS `amount` FROM t2 GROUP BY id
) `x`
GROUP BY `id`
I groupped each table and unioned because i think it might be faster, but you should try both solutions.
Solution 3
Subquery:
SELECT id, SUM(amount)
FROM ( SELECT * FROM t1
UNION ALL SELECT * FROM t2
UNION ALL SELECT * FROM t3
)
GROUP BY id
Solution 4
As it's not very clear from previous answers, remember to give aliases (on MySQL/MariaDb) or you'll get error:
Every derived table must have its own alias
select id, sum(amount) from (
select id,amount from table_1 union all
select id,amount from table_2 union all
select id,amount from table_3
) AS 'aliasWhichIsNeeded'
group by id
Solution 5
Not sure if MySQL uses common table expression but I would do this in postgres:
WITH total AS(
SELECT id,amount AS amount FROM table_1 UNION ALL
SELECT id,amount AS amount FROM table_2 UNION ALL
SELECT id,amount AS amount FROM table_3
)
SELECT id, sum(amount)
FROM total
I think that should do the trick as well.
Itay Moav -Malimovka
SOreadytohelp Below are some of the open source projects I work on. A PHP Library the wrappes SurveyMonkey's API https://github.com/itay-moav/TheKofClient A tool to Schema Check, manage Stored Procedures, Triggers, Views and get autocompletion: https://github.com/itay-moav/rahl_commander A fun way to point users at the right direction in your site ;-) https://github.com/itay-moav/babahandofgod An old version of WMD which I converted to Mootools, 8 years ago... http://moowmd.awardspace.info Feel free to contact me through linkedin http://www.linkedin.com/in/itaymoav
Updated on July 09, 2022Comments
-
Itay Moav -Malimovka almost 2 years
I have a union of three tables (t1,t2,t3). Each rerun exactly the same number of records, first column is id, second amount:
1 10 2 20 3 20 1 30 2 30 3 10 1 20 2 40 3 50
Is there a simple in sql way to sum it up to only get:
1 60 2 80 3 80
-
David Oneill over 14 yearsNote: you shouldn't use select * in this case.
-
zerkms over 14 yearsNote 2: I bet mysql will ask you to assign alias to subquery
-
adharris over 14 years@David: For sure, my bad. Probably doesn't matter if id and amount are the only two columns, but what are the chances of that? @zerkms: Also true.
-
David Oneill over 14 yearsexactly. And even if there are only id and amount for now, there's no sense in shooting yourself in the foot when you add a third column in 6 months... :)
-
Peter Kirby about 10 yearsI'm looking for a way to accomplish this in a view, but sub-queries aren't allowed in views. Is there an alternative?
-
Boti about 10 yearsmake the subquery another view
-
just_myles about 4 yearsAs was mentioned. This requires a group by id.
-
Hamza Rahman over 2 years@EricAya I have already changed the letter please check. Thank you