UNION查询错误 - “您已尝试执行不包含指定表达式的查询...”

时间:2013-12-18 20:14:16

标签: sql ms-access union

我正在尝试通过UNION组合多个查询。当我尝试执行时,我收到以下错误:

“您尝试执行的查询不包含指定表达式'DateRec'作为聚合函数的一部分。”

有什么想法吗?

这是我的查询。

SELECT [Leads Received by Agent].DateRec, [Leads Received by Agent].AgentID, 
    [Leads Received by Agent].AgentFirstName, [Leads Received by Agent].AgentLastName, 
    Count([Leads Received by Agent].LastAction) AS CountOfLastAction, 
    'Leads Received by Agent' as type 
FROM [Leads Received by Agent]

UNION ALL

SELECT [Leads Proposed by Agent].DateRec, [Leads Proposed by Agent].AgentID, 
    [Leads Proposed by Agent].AgentFirstName, [Leads Proposed by Agent].AgentLastName, 
    Count([Leads Proposed by Agent].LastAction) AS NumofLeadsProp, 
    'Leads Proposed by Agent' 
FROM [Leads Proposed by Agent]

ORDER BY [Leads Received by Agent].DateRec;

1 个答案:

答案 0 :(得分:2)

如下所示,在union的每一半中添加group by子句,例如......

SELECT [Leads Received by Agent].DateRec, 
       [Leads Received by Agent].AgentID, 
       [Leads Received by Agent].AgentFirstName, 
       [Leads Received by Agent].AgentLastName, 
       Count([Leads Received by Agent].LastAction) AS CountOfLastAction, 
       'Leads Received by Agent' as type 
FROM [Leads Received by Agent]
GROUP BY [Leads Received by Agent].DateRec, 
         [Leads Received by Agent].AgentID, 
         [Leads Received by Agent].AgentFirstName, 
         [Leads Received by Agent].AgentLastName,'Leads Received by Agent' as type 

另外,我不建议将“type”作为列别名is a reserved word