在MS Access中,我试图获取平均数据,但仅在每组的最后一条记录大于某个值时。几乎拥有它,但不能得到最后一部分。所以我的数据看起来像这样:
CarID WeekOf NumDataPoints Fuel
3AA May-14-2011 4 300
7BB May-14-2011 9 250
3AA May-21-2011 35 310
7BB May-21-2011 7 275
3AA May-28-2011 24 355
7BB May-28-2011 4 280
我的记录集应该包括那些当前点数> = 5的汽车的平均燃料。因此当前周是2011年5月28日,并且唯一一个点数> = 5的汽车是车3AA。在其他几周里,点数是多少并不重要。我试过了
SELECT CarId, Avg(Fuel) AS AvgF
FROM tblCars
WHERE WeekOf>=#5/14/2011# And WeekOf<=#5/28/2011# AND
CarId = (SELECT CarId FROM tblCars
WHERE WeekOf=#5/28/2011# AND NumDataPoints>=5)
GROUP BY CarId;
我应该回来
CarID AvgF
3AA 321.6666
我如何仅在本周提出一个标准,但是当它通过标准时会回来三周?
答案 0 :(得分:1)
您可以使用此查询来显示哪些车辆有5个或更多NumDataPoints。
SELECT CarID FROM tblCars
WHERE
(((WeekOf)=#2011-05-28#)
AND ((NumDataPoints)>=5));
这可以平均每辆车在3周范围内的燃油值。
SELECT CarID, Avg(Fuel) AS AvgF
FROM tblCars
WHERE
(((WeekOf)>=#2011-05-14#
And (WeekOf)<=#2011-05-28#))
GROUP BY CarID;
INNER在CarID上加入它们,以便第一个查询从第二个查询中选择汽车的子集。
SELECT whichcars.CarID, fuel_averages.AvgF
FROM
[SELECT CarID FROM tblCars
WHERE
(((WeekOf)=#2011-05-28#)
AND ((NumDataPoints)>=5))
]. AS whichcars
INNER JOIN [SELECT CarID, Avg(Fuel) AS AvgF
FROM tblCars
WHERE
(((WeekOf)>=#2011-05-14#
And (WeekOf)<=#2011-05-28#))
GROUP BY CarID
]. AS fuel_averages
ON whichcars.CarID = fuel_averages.CarID;