Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Sum two (or more) queries
Fri, Feb 5 2021 10:34 AMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Hi everyone

I would like to know if there is a way to sum two queries like I tried to describe below:

select a, sum(tot) from
(
select a, tot from table1
union all
select a, tot from table2
)

Any idea ?

Regards
Eduardo
Sat, Feb 6 2021 3:35 AMPermanent Link

Ian Branch

Avatar

Hi Jose,


Regards
Eduardo
Sat, Feb 6 2021 3:41 AMPermanent Link

Ian Branch

Avatar

Hi Jose,
I think you are going to need something like this..

Select a, sum(tot) from
(
select a, tot from table1
union all
select a, tot from table2
) Tbl group by a

Assuming a is the field you want to sum against.

If a isn't what you want to group/sum against then..

Select a, grpfield, sum(tot) from
(
select a, grpfield, tot from table1
union all
select a, grpfield, tot from table2
) Tbl group by grpfield

HTH,
Ian
Sat, Feb 6 2021 7:50 AMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Ian

I was close to the solution but I forgot the table reference (tbl) in the outer group by.

Thank you very much for your tip.

Eduardo
Image