Bringing added records from another table in a new query

0

I have a table "tbA" where the primary column "a1" needs to be referenced by the "tbB" table by the "fk_a1" column

tbA

id | nome   |
---+------+--
1,  'aaaa'
2,  'bbbbb'
3,  'cccc'
4,  'ddddd'

tbB

id | fk_a1 |  valor_1 | valor_2 |
---+-------+----------+----------
1,     2,     500.00,    100.00
2,     1,     150.00,    200.00
3,     1,     100.00,    50.00
4,     3,     10.00,     0.00
5,     3,     100.00,     20.00

Considering that the relationship between the two tables is something like

SELECT * FROM tbA a LEFT JOIN tbB b ON b.fk_a1 = a.id

Once this relationship is complete, list the records, assuming the main table is tbA and the tbB summed values will come in the list as

id | nome |  soma_1 | soma_2 |
---+------+---------+---------
1,  'aaaa',   250.00,  50.00
2,  'bbbbb',  500.00,  100.00
3,  'cccc',   110.00,  20.00
4,  'ddddd',  0.00,    0.00

I do not know exactly how to do this, thank you.

    
asked by anonymous 27.04.2016 / 22:43

1 answer

1

It would be something + - like this:

SELECT id,nome,SUM(valor_1) as soma_1, SUM(valor_2) as soma2 
FROM tbA a JOIN tbB
ON b.fk_a1 = a.id, GROUP BY a.id

I removed LEFT because in that case it would have no effect.

    
27.04.2016 / 22:47