SQL新手,但我很确定我的查询是正确的。我已经尝试了一千种不同的方法但它仍然有时会将我的列/别名lead_ID_t视为参数。我把所有东西放在方括号内试图提供帮助,但无济于事......
我在notepad ++中编辑查询,当我将其粘贴到Access中时,它运行得很好。然后我保存并关闭查询。当我尝试再次运行它时,它将lead_ID_t视为参数,我的格式完全消失了......任何人都可以解释发生了什么?
SELECT [Total].[lead_ID_t] AS [Lead ID],
IIF(isnull([Total].[lead_count_t]-[F9].[lead_count_f]),0,[Total].[lead_count_t]-[F9].[lead_count_f]) AS [V Calls]
FROM (SELECT DISTINCT [Lead_ID] AS [lead_ID_t], Count([Lead_ID]) AS [lead_count_t]
FROM (SELECT * FROM [Logs] WHERE [log_actor] <> "Five9 Dialer") As [TData] GROUP BY [Lead_ID]) AS [Total]
LEFT JOIN (SELECT DISTINCT [Lead_ID] AS [Lead_ID_f], count([Lead_ID]) AS [lead_count_f]
FROM (SELECT * FROM [Logs] WHERE [log_actor] = "Five9 Dialer") As [FData] GROUP BY [Lead_ID]) AS [F9] ON [Total].[lead_ID_t]=[F9].[lead_ID_f];
PS。使用和不使用iif语句时会发生同样的错误
答案 0 :(得分:0)
考虑调整后的紧凑查询:
删除多余的嵌套级别,因为您可以在聚合WHERE
查询中使用GROUP BY
。
删除DISTINCT
,因为汇总查询已经返回不同的分组值。
替换IIF()
的{{1}}(NULL
为0的语句)。
虽然 Total 可能不是列出的MS Access或JET保留字,但它可能是GUI应用程序保留字,从而导致问题。使用以前的子查询别名 TData 和 FData 。
或者,对没有子查询或连接的一个主查询使用条件聚合,并且可以使用计算列来避免重复。
紧凑查询
NZ()
条件聚合
SELECT [TData].[lead_ID_t] AS [Lead ID],
NZ(([TData].[lead_count_t] - [FData].[lead_count_f]),0) AS [V Calls]
FROM
(SELECT [Lead_ID] AS [lead_ID_t],
COUNT([Lead_ID]) AS [lead_count_t]
FROM [Logs]
WHERE [log_actor] <> 'Five9 Dialer'
GROUP BY [Lead_ID]) AS [TData]
LEFT JOIN
(SELECT [Lead_ID] AS [Lead_ID_f],
COUNT([Lead_ID]) AS [lead_count_f]
FROM [Logs]
WHERE [log_actor] = 'Five9 Dialer'
GROUP BY [Lead_ID]) AS [FData]
ON [TData].[lead_ID_t] = [FData].[lead_ID_f];