There's a fair bit going on in that query. Suggestions:
1. To get the subquery working correctly, create a new query into just the
Orders table. Type the subquery into the field, and get it working. Once it
is working, you can copy the expression into the more involved query.
2. Now you have the subquery working, replace the bad one in your original
query with this one. In the Total row of this subquery field, change Group
By to Expression.
3. Remove the Payments table from this query. This will cause it to drop out
the payment number, amount, essettdate, and essettnumber fields. Presumably
you don't need these if you are wanting the total value of payments instead
of the individual payments.
4. It looks like payments are credited against the OrderID, yet OrderID is
not one of the fields your query is grouping on. The query should show the
correct total payments for the order, but if it is not showing which order
that is, I'm not sure the data has any meaning.
5. If you do add the OrderID field to the output grid and Group By this
field, the query should should the correct total payments received for the
order. However, if the OrderID is duplicated (because of grouping on the
other fields) and you sum the amounts, you will still have duplicated
totals.
6. The HAVING clause is not going to work like that. If you are actually
trying to go the other direction, and group by payments, you could use the
subquery on the order information.
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
jwrnana said:
This is the SQL for the Query that I am using. I tried to remove the
Payments table, but got errors when I typed in the SQL as you suggested in
a
fresh field in my query. I get syntax error. I changed your suggestion
to
read with the correct spelling, etc.
SELECT Payments.Payment,
Contracts.ContractNum,
SIN.SIN,
Products.ProductCode,
Products.ProductName,
[Order Details].Quantity,
Sum([Order Details].UnitPrice) AS SumOfUnitPrice,
Customers.ControlNum,
Payments.PaymentAmount,
Payments.EssettDate,
Payments.EssettNumber,
(SELECT CCur(Nz(Sum(Payments.PaymentAmount).0))
FROM Payments WHERE Payments.OrderID = Orders.OrderID) AS Received
FROM ((Customers INNER JOIN Orders ON Customers.CustomerID =
Orders.CustomerID)
INNER JOIN ((Contracts INNER JOIN ContractSIN_XRef
ON Contracts.ContractID = ContractSIN_XRef.ContractID)
INNER JOIN ((SIN INNER JOIN Products ON SIN.SINID = Products.SINID)
INNER JOIN [Order Details] ON Products.ProductID = [Order
Details].ProductID)
ON (SIN.SINID = ContractSIN_XRef.SINID) AND (Contracts.ContractID =
Products.ContractID))
ON Orders.OrderID = [Order Details].OrderID) LEFT JOIN Payments
ON Orders.OrderID = Payments.OrderID
GROUP BY Payments.PaymentDate,
Contracts.ContractNum,
SIN.SIN,
Products.ProductCode,
Products.ProductName,
[Order Details].Quantity,
Customers.ControlNum,
Payments.PaymentAmount,
Payments.EssettDate,
Payments.EssettNumber,
(SELECT CCur(Nz(Sum(Payments.PaymentAmount).0))
FROM Payments WHERE Payments.OrderID = Orders.OrderID)
HAVING (((Payments.PaymentDate)>=[forms]![Report Date Range]![Beginning
Order Date]
And (Payments.PaymentDate)<=[forms]![Report Date Range]![Ending Order
Date]))
ORDER BY Contracts.ContractNum DESC;