I have a Select query that returns 15 records. The three fields are Year, Qty, Amount. When I convert this to a Totals query grouping on Year, Averaging the Amount and Counting the Qty the query comes back with a Count of 16. How is this possible?
Changeing the SQL statement from Select to Select Distinct did not change the results in the Totals query. It did group the Select query to 4 records based on 4 different Amounts.
My query is actually more involved than I originally stated. I did not want to complicate the question with possible erroneous information. Anyway here is the Totals SQL:
SELECT dbo_HistoryAll.PartNo, Year([TranDate]) AS Yr, Count(dbo_HistoryAll.Qty) AS CountOfQty, Avg(dbo_HistoryAll.TranAmt) AS AvgOfTranAmt
FROM dbo_HistoryAll
WHERE (((dbo_HistoryAll.DocType)="fr") AND ((dbo_HistoryAll.CurAssn)="ac"))
GROUP BY dbo_HistoryAll.PartNo, Year([TranDate])
HAVING (((dbo_HistoryAll.PartNo)="ag44a") AND ((Year([TranDate]))=2004) AND ((Avg(dbo_HistoryAll.TranAmt))>0));
Here is the Select SQL:
SELECT dbo_HistoryAll.PartNo, Year([TranDate]) AS Yr, dbo_HistoryAll.Qty, dbo_HistoryAll.TranAmt
FROM dbo_HistoryAll
WHERE (((dbo_HistoryAll.PartNo)="ag44a") AND ((Year([TranDate]))=2004) AND ((dbo_HistoryAll.TranAmt)>0) AND ((dbo_HistoryAll.DocType)="fr") AND ((dbo_HistoryAll.CurAssn)="ac"));
Your Totals query doesn't aggregate the same rows as those returned by your Select query.
To get a consistent result:
SELECT dbo_HistoryAll.PartNo, Year([TranDate]) AS Yr, Count(dbo_HistoryAll.Qty) AS CountOfQty, Avg(dbo_HistoryAll.TranAmt) AS AvgOfTranAmt
FROM dbo_HistoryAll
WHERE (((dbo_HistoryAll.PartNo)="ag44a") AND ((Year([TranDate]))=2004) AND ((dbo_HistoryAll.TranAmt)>0) AND ((dbo_HistoryAll.DocType)="fr") AND ((dbo_HistoryAll.CurAssn)="ac"))
GROUP BY dbo_HistoryAll.PartNo, Year([TranDate])
Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.