1

I have 3 Tables:


_tbl1_____
ID  C1  C2
--  --  -- 
AA   2   3
BB  10  12
CC  20  22

_tbl2_____
ID  C1  C2
--  --  -- 
BB  10  12
AA   2   3
CC  20  22

_tbl3_____
ID  C1  C2
--  --  -- 
AA   0   0
BB  10  12
HH  55  66

and I want sum the columns with same name and rows with same id to get a result like this:


Query result tbl
ID  C1  C2
--  --  -- 
AA   4   6
BB  30  36
CC  40  44
HH  55  66

Thank you guys for your help.. I really need it.

1

If they're all in the same order, you can do it easily like this,

SELECT t.id, sum(c1), sum(c2)
FROM (
  SELECT *
  FROM tbl1
  UNION ALL ( TABLE tbl2 )
  UNION ALL ( TABLE tbl3 )
) AS t
GROUP BY t.id
ORDER BY 1;

If they're in different orders just replace TABLE tbl2 with SELECT id, c1, c2 FROM tbl2 (they're the same thing).

  • Than you very much Evan.. – Vera Tiago Mar 25 '18 at 14:13
  • 1
    sorry the long time. Other things got in the way of getting back to this.. – Vera Tiago Mar 25 '18 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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