0

I have three tables.

A

ID   NAME  
---  ----   
1    abc   
2    asd  
3    qwe

B

ID    INCOME  
---   ------  
1      2  
2      3  
1      4

C

NAME   TOTAL  
----  ----    
abc    8  
asd    20

I want to join this three tables with a SQL query to produce an output like

ID INCOME TOTAL  
---------------
1    6       8  
2    3      20

Could anyone help me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3513024
  • 1
  • 1
  • 3
  • Possible duplicate of [SQL Inner-join with 3 tables?](https://stackoverflow.com/questions/10195451/sql-inner-join-with-3-tables) – Cyber Progs Jul 27 '17 at 22:30

1 Answers1

0

You could try this:

select 
    t1.id, sum(t2.income) income, sum(t3.total) total
from 
    table1 t1 
join 
    table2 t2 on t1.id = t2.id
join 
    table3 t3 on t3.name = t1.name
group by 
    t1.id
order by 
    t1.id
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rigerta
  • 3,959
  • 15
  • 26