'Get the sum of child's tables fields in postgresql
I have table scheme as below:
Table A:
id field_a1:integer
1 100
Table B
id a1_id:integer field_b1
1 1 201
2 1 202
Table C
id a1_id:integer field_c1
1 1 200
I need sql query to get the SUM of Table A and B fields so result should be as bellow
SQL results(should be):
table_A_field_a1 total_sum(field_c1+field_b1)
100 603(200+201+202)
my sql query is
SELECT (SUM(field_c1)+SUM(field_b1)) total_sum, a1.*
FROM A as a
LEFT JOIN B as b on b.a1_id = a.id
LEFT JOIN C as c on c.a1_id = a.id
GROUP BY a.id
But i am getting total_sum = 803 but it should be 603
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
