View Full Version : Complex Aggregate Function


Ken Grubb
03-07-2002, 03:13 PM
I have a table of data containing, among other things, a Date/Time field I call NetTime. I have an Aggregate Query returning Count, Average, Max, Min, and StDev for NetTime.

What I'm trying to write now is an Aggregate Query that will return a Count of NetTime where NetTime is greater than the Average of NetTime.

Any help would be appreciated.

Harry
03-08-2002, 02:50 AM
Ought to be better answer than this, but how about using the Average value in a new query and select records >Average. Then create a second query which counts the records from the first.

anna the third
03-08-2002, 04:11 AM
Hi,
Try something like the following query. I have tested it and it works just fine. There is one "but": This only works if the second select statement returns exactly one row!!!

SELECT Count(NetTime)
FROM myTable
WHERE NetTime >
(
SELECT Avg(NetTime) FROM mytable
)

If this is not what you are looking for
let me know
anna

[This message has been edited by anna the third (edited 03-08-2002).]

Ken Grubb
03-08-2002, 05:44 AM
Thanks to both Harry and anna the third. Both of your input gave me the solution. I think I neglected to explain some essential pieces of what I needed this Query to provide.

I started with the Query qryComplexAvgTimes:
SELECT Status, Delivery, Format(Avg(NetTime),"hh:nn:ss") AS AvgOfNetTime
FROM tblFaxData
GROUP BY Status, Delivery;

Then added the Query qryComplexList:
SELECT tblFaxData.Status, tblFaxData.Delivery, tblFaxData.NetTime
FROM qryComplexAvgTimes
INNER JOIN tblFaxData ON (qryComplexAvgTimes.Delivery = tblFaxData.Delivery)
AND (qryComplexAvgTimes.Status = tblFaxData.Status)
WHERE (((tblFaxData.NetTime)>[AvgOfNetTime]));

And finally added the Query qryComplexFinal:
SELECT Status, Delivery, Format(Count(NetTime),"#,###") AS CountOfNetTime
FROM qryComplexList
GROUP BY Status, Delivery;