我有一个查询,当我输入不匹配的日期时,我收到此错误。例如,如果我的日期范围是2014年1月1日 - 2014年3月1日,它可以正常工作。如果我将日期范围更改为2014年1月31日 - 2014年2月28日,则会出现此错误。只要日期与它的数量相同即可。我完全难过了。有任何想法吗?这是我正在使用的查询:
PARAMETERS [START DATE] DateTime, [END DATE] DateTime;
SELECT DISTINCT VEHICLE.VID, VEHICLE.VLOCATION, FUELTRAN.TRANDATE,
(SELECT FUELTRAN.ODOMETER
FROM FUELTRAN
WHERE VEHICLE.VID = FUELTRAN.VID
AND (TRANDATE BETWEEN [START DATE] AND [END DATE])
AND TRANID = (SELECT MAX(TRANID)
FROM FUELTRAN
WHERE VEHICLE.VID = FUELTRAN.VID
AND (TRANDATE BETWEEN [START DATE] AND [END DATE])
AND FUELTRAN.ODOMETER > 0)) AS CURRENTODOM,
(SELECT FUELTRAN.ODOMETER
FROM FUELTRAN
WHERE VEHICLE.VID = FUELTRAN.VID
AND (TRANDATE BETWEEN [START DATE] AND [END DATE])
AND TRANID = (SELECT MIN(TRANID)
FROM FUELTRAN
WHERE VEHICLE.VID = FUELTRAN.VID
AND (TRANDATE BETWEEN [START DATE] AND [END DATE])
AND FUELTRAN.ODOMETER > 0)) AS INITIALODOM,
(SELECT SUM(FUELTRAN.GALLONS)
FROM FUELTRAN
WHERE FUELTRAN.VID = VEHICLE.VID
AND (TRANDATE BETWEEN [START DATE] AND [END DATE])) AS TOTALGALLONS,
((CURRENTODOM-INITIALODOM)/TOTALGALLONS) AS MPG
FROM VEHICLE, FUELTRAN
WHERE VEHICLE.VID=FUELTRAN.VID And TRANDATE=[END DATE] And (VEHICLE.VTYPE="STRAIGHT TRUCK" Or VEHICLE.VTYPE="TRACTOR")
ORDER BY VEHICLE.VLOCATION, VEHICLE.VID, FUELTRAN.TRANDATE;
答案 0 :(得分:0)
也许尝试更简单的查询,例如:
PARAMETERS STARTDATE DateTime, ENDDATE DateTime;
SELECT VEHICLE.VID, VEHICLE.VLOCATION, VEHICLE.VTYPE, Sum(FUELTRAN.GALLONS) AS TOTALFUEL,
Max([ODOMETER])-Min([ODOMETER]) AS MILAGE, [MILAGE]/[TOTALFUEL] AS MPG
FROM FUELTRAN INNER JOIN VEHICLE ON FUELTRAN.VID = VEHICLE.VID
WHERE (((FUELTRAN.TRANDATE) Between [STARTDATE] And [ENDDATE]))
GROUP BY VEHICLE.VID, VEHICLE.VLOCATION, VEHICLE.VTYPE
HAVING (((VEHICLE.VTYPE)="STRAIGHT TRUCK" Or (VEHICLE.VTYPE)="TRACTOR"));
在过去使用多个子查询时遇到了类似的问题。