查询不包含指定的函数

时间:2013-10-04 19:38:48

标签: sql ms-access ms-access-2010

我正在尝试在MS Access 2010数据库中运行以下查询:

SELECT a.[Level], max(a.dte) AS nextDate, IIf(a.[Type1Date]<a.[Type2Date],"t1","t2") AS Type
FROM (
    select [Level], Type1Date as dte, Type1Date, Type2Date 
    FROM CommunicationTable WHERE ClientNumber=1
    UNION
    select [Level], Type2Date as dte, Type1Date, Type2Date 
    FROM CommunicationTable WHERE ClientNumber = 1
)  AS a
GROUP BY a.[Level];  

但是,Access正在给我一个对话框,说明:

You tried to execute a query that does not include the specified  
expression 'IIf(a.[Type1Date]<a.[Type2Date],"t1","t2")' as part  
of an aggregate function.  

任何人都可以解释这意味着什么,并告诉我如何修复代码,以便它返回我要求的字段(Level,nextDate,Type)?

1 个答案:

答案 0 :(得分:4)

使用组查询时,不能在Select语句中包含列,除非它们也在Group By语句中或聚合中。所以你可能想要使用:

GROUP BY a.[Level], IIf(a.[Type1Date]<a.[Type2Date],"t1","t2")