0

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.

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

3 Comments

sqlfiddle.com/#!3/66912/1/0 - you beat me to it, so I put your query in an sql fiddle.
Well I have tried several combinations, Thanks for your answer the I am getting different results however if I just Select SUM(t3.TotalInvoice) where FID = 9 is exactly "the amount of times 9 is in the FID field" divided by the Value I get when I run your query.
Hi. I'm not sure what these problem is. Check out the sqlfiddle in the comment Derek left. The query works there. Try to figure out what is different with your code or data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.