Adding records with type_type in another table

2

I have 2 tables linked by id and I need to add a field in table 2 by grouping through a field in table 1

-----TABELA 1-----
ID_tab1 - tipo_pgto

-----TABELA 2-----
ID_tab2 - ID_tab1 - valor_pago

I want you to show me the sum of the amount paid that is in table 2 but grouped by type_pgto that is in table 1

    
asked by anonymous 17.11.2015 / 05:18

1 answer

0

Try this:

select tipo_pgto, sum(valor_pago) from tab1 inner join
tab2 on tab1.ID_tab1 = tab2.ID_tab1_fk group by tipo_pgto

Q: I added fk there only so it is easy to identify that it is a foreign key coming from table 1.

Basically a select with inner join, joining the two tables, grouping and summing by type of payment.

See working in SQLFIDDLE (updated link)

References:

link

link

    
17.11.2015 / 11:19