gurugarzah
gurugarzah

Reputation: 3

Multi Table Join adding columns SQL Server

Table1

ID      FID    DID
1       91     9
2       92     9
3       34     862

Table2

FID      Office Name
9        CompABC
862      CompXYZ

Table3

FID       TotalInvoice
91        850
91        450
91        450
92        450
34        300
34        325

The results from the query I am trying to achieve are like so

9    CompABC 2200
862  CompXYZ 625

I have tried something like

Select SUM(t3.TotalInvoice) as InvoiceTotal
,t2.[Office Name]
from Table2 t2
inner join table1 t1 on t2.FID = t1.DID
inner join table3 t3 on t1.FID = t3.FID

DID are actually entities of Table1 so a full list would include itself.

I am getting improper results here any help would be appreciated.

Upvotes: 0

Views: 304

Answers (1)

zigdawgydawg
zigdawgydawg

Reputation: 2046

Try this:

select t2.FID, t2.[Office Name], SUM(t3.TotalInvoice) 
from Table2 t2
join Table1 t1 on t2.FID = t1.DID
join Table3 t3 on t1.FID = t3.FID
group by t2.FID, t2.[Office Name]

Upvotes: 1

Related Questions