jimfortune@compumarc.com wrote:[color=blue]
> After writing the following information:
>
> :StartInfo
>
> [gas info]
> GIID VehicleID ReceiptDate Amount Mileage Gallons
> 1 3 11/1/05 32 15500 15
> 2 3 11/7/05 37.5 15800 16.2
> 3 3 11/12/05 34 16100 15.7
> 4 4 11/2/05 39.04 22001 18.3
> 5 4 11/9/05 45.68 22555 21.2
>
>
> The goal is to get MPG values for each record in a query assuming that
> the report can group on the VehicleID and do some totaling. If not, we
> can create other queries that can be used by the report to get the
> totals using a public function in the total box's ControlSource by
> passing the SQL string for the totals query to the function.
>
> qryComputeMPG:
> SELECT GIID, VehicleID, ReceiptDate, Amount, Mileage, Gallons,
> ((SELECT A.Mileage FROM [gas info] AS A WHERE A.VehicleID = [gas
> info].vehicleID AND A.ReceiptDate IN (SELECT Max(B.ReceiptDate) FROM
> [gas info] As B WHERE B.ReceiptDate < [gas info].ReceiptDate AND
> B.VehicleID = [gas info].VehicleID))) AS [PrevMileage],
> IIf(IsNull([PrevMileage]), Null, (Mileage - Nz([PrevMileage])) /
> Gallons) AS MPG FROM [gas info] ORDER BY VehicleID, ReceiptDate;
>
> !qryComputeMPG:
> GIID VehicleID ReceiptDate Amount Mileage Gallons PrevMileage MPG
> 1 3 11/1/05 32 15500 15 Null Null
> 2 3 11/7/05 37.5 15800 16.2 15500 18.518...
> 3 3 11/12/05 34 16100 15.7 15800 19.108...
> 4 4 11/2/05 39.04 22001 18.3 Null Null
> 5 4 11/9/05 45.68 22555 21.2 22001 26.132...
>
> The problem with simply excluding records for the report from
> qryComputeMPG that have Null MPG values is that you lose the gallons
> for the first record of the month for that vehicle if you do. Maybe
> IIf() can be used in the totals to weed this case out. If another
> query is required to obtain the totals:
>
> qryTotalPerVehicle:
> SELECT qryComputeMPG.VehicleID, CCur(Sum(qryComputeMPG.Amount)) AS
> TotalAmount, Sum(qryComputeMPG.Gallons) AS TotalGallons, (SELECT
> Avg(Val(A.MPG)) FROM qryComputeMPG As A WHERE A.VehicleID =
> qryComputeMPG.VehicleID AND A.MPG IS NOT NULL) AS AvgMPG FROM
> qryComputeMPG WHERE (((qryComputeMPG.ReceiptDate)>=#11/1/2005# And
> (qryComputeMPG.ReceiptDate)<=#11/30/2005#)) GROUP BY
> qryComputeMPG.VehicleID;
>
> !qryTotalPerVehicle:
> VehicleID TotalAmount TotalGallons AvgMPG
> 3 $103.50 46.9 18.8133993866
> 4 $84.72 39.5 26.1320754717
>
> :EndInfo
>
> I created a Mileage.mdb database to continue trying out these ideas. I
> didn't feel that what I had come up with so far was adequate because of
> the way the report errored at the grouping. Also, I didn't like the
> stipulations for joining AccessMonster.com so I didn't try to answer
> the question directly there.
>
> James A. Fortune[/color]
In an unrelated post, Randy Harris said:
<You should check out the Domain Aggregate Functions - DMax, DMin,
DAvg, etc
I will use DMax or DAvg if it turns out to be absolutely necessary. It
should solve the problem though.
James A. Fortune