如果自行车站满或超过10%,我试图显示警告。当我运行此查询时,我得到"您正在尝试执行不包含iif语句的查询...作为聚合函数的一部分。
Bike_locations表 - Bicycle_id和Locations_ID
位置表 - Locations_ID,No_of_Spaces,Location_Address
SELECT Locations.Location_Address, Count(Bike_Locations.Bicycle_ID) AS CountOfBicycle_ID,
IIf(((([CountOfBicycle_ID]/[LOCATIONS]![No_Of_Spaces])*100)>90),"This Station is nearly full.
Need to move some bicycles out of here",IIf(((([CountOfBicycle_ID]/[LOCATIONS]![No_Of_Spaces])*100)
<10),"This station is nearly empty. Need to move some bicycles here","")) AS Warnings
FROM Locations INNER JOIN Bike_Locations ON Locations.[LOCATIONS_ID] = Bike_Locations.[LOCATIONS_ID]
GROUP BY Locations.Location_Address;
任何人都有一个scooby
答案 0 :(得分:0)
当您使用GROUP BY时,您应该在SELECT和GROUP BY语句中具有完全相同的字段,但只应在SELECT
中指定的聚合函数除外您的案例中的聚合函数是COUNT(*)
您聚合的字段是:
错误消息告诉您两个语句中没有相同的内容。
2个解决方案:
请注意,在MS Access SQL中,您不能(遗憾地)在GROUP BY中使用SELECT中指定的别名。所以你必须复制整个字段,这将是你的情况下的长iif
编辑:更好的解决方案提案:
我会从根本上改变你的方法,因为你不会在所有那些嵌套iff
的地方创建以下查询并为其命名(例如) Stations_Occupation
SELECT L.Locations_ID AS ID,
L.Location_Address AS Addr,
L.No_of_Spaces AS TotSpace,
BL.cnt AS OccSpace,
ROUND((BL.cnt/L.No_of_Spaces*100),0) AS OccPourc
FROM Locations L
LEFT JOIN
(
SELECT Locations_ID, COUNT(*) AS cnt
FROM Bike_Locations
GROUP BY LOCATIONS_ID
) AS BL ON L.Locations_ID = BL.Locations_ID
此查询可能会在您的应用程序的许多部分中提供很多帮助,而不仅仅是在这里,因为它会计算每个工作站的占用百分比
一些例子:
获取占有率> 90%的所有电台:
SELECT Addr
FROM Stations_Occupation
WHERE OccPourc > 90
获取所有<10%职业的电台:
SELECT Addr
FROM Stations_Occupation
WHERE OccPourc < 10
获取特定电台的职业级别:
SELECT OccPourc
FROM Stations_Occupation
WHERE ID=specific_station_ID
获取特定电台的自行车数量和最大值:
SELECT OccSpace & "/" & TotSpace
FROM Stations_Occupation
WHERE ID=specific_station_ID