Hi, I am working on a project but I am running into trouble with one of the queries:
SELECT Stock.[Lot Number], Stock.Stone, Stock.Shape, Stock.Size, Stock.Cut, Stock.Mins, Stock.[Color], Sum(Query1.AdjQty) AS Stock, Sum(Query1.TotalAmt)/Sum(Query1.AdjQty) AS [Average Cost], Sum(Query2.TotalAmount)/Sum(Query2.AdjustedQty) AS [Purchase Price]
FROM (Stock INNER JOIN
(SELECT Transactions.[Transaction ID], Transactions.[Lot Number], Transactions.Date, Transactions.[Transaction Type], Transactions.Quantity, Transactions.[Price], IIf([Transaction Type]="Sale",[Transactions.Quantity]*-1,[Transactions.Quantity]) AS AdjQty, AdjQty*Transactions.[Price] AS TotalAmt FROM Transactions
GROUP BY Transactions.[Transaction ID], Transactions.[Lot Number], Transactions.Date, Transactions.[Transaction Type], Transactions.Quantity, Transactions.[Price], IIf([Transaction Type]="Sale",[Transactions.Quantity]*-1,[Transactions.Quantity])) AS Query1
ON Stock.[Lot Number] = Query1.[Lot Number])
INNER JOIN
(SELECT Transactions.[Transaction ID], Transactions.[Lot Number], Transactions.Date, Transactions.[Transaction Type], Transactions.Quantity, Transactions.[Price], IIf([Transaction Type]="Sale",[Transactions.Quantity]*0,[Transactions.Quantity]) AS AdjustedQty, AdjustedQty*Transactions.[Price (THB)] AS TotalAmount FROM Transactions
GROUP BY Transactions.[Transaction ID], Transactions.[Lot Number], Transactions.Date, Transactions.[Transaction Type], Transactions.Quantity, Transactions.[Price], IIf([Transaction Type]="Sale",[Transactions.Quantity]*0,[Transactions.Quantity])) AS Query2
ON Stock.[Lot Number] = Query2.[Lot Number]
GROUP BY Stock.[Lot Number], Stock.Stone, Stock.Shape, Stock.Size, Stock.Cut, Stock.Mins, Stock.[Color];
Maybe its impossible to tell anything from this mess of code but I am hoping its just a syntax issue that I am missing. The problem is the value returned by 'Sum(Query1.AdjQty) AS Stock ' is wrong. I just can't figure out what I am missing.
I have already tried separating these into separate queries and still getting the same results.