I'm sorry for the bad Subject, but didn't know how to put it.I made a small example of the problem I'm facing to explain. I'm sure this is a common problem, but my SQL knowledge is to limited to come up with a solution.Please consider the following example
declare @TableA table(ID int, OrderNr varchar(100))declare @TableB table(ID int, FKA int, Quantity decimal, Price decimal)declare @TableC table(ID int, OrderNumber varchar(100), Quantity decimal, Price decimal)insert into @TableAvalues(1,'1234')insert into @TableBvalues(1,1,2,2),(2,1,3,3),(3,1,4,4)insert into @TableCvalues(1,'1234',5,5),(2,'1234',6,6)-------------------------------------------select SUM(B.Quantity * B.Price)from @TableA as Ainner join @TableB as B on B.FKA = A.IDwhere A.ID = 1select SUM(C.Quantity * C.Price)from @TableA as Ainner join @TableC as C on C.OrderNumber = A.OrderNrwhere A.ID = 1-------------------------------------------select SUM(B.Quantity * B.Price), SUM(C.Quantity * C.Price)from @TableA as Ainner join @TableB as B on B.FKA = A.IDinner join @TableC as C on C.OrderNumber = A.OrderNr where A.ID = 1
The first two queries give the correct result, but when I try to combine them into one query, the sums get multiplied with the number of rows of the other join.I have no idea how to prevent this. Can somebody please shed some light?