我需要创建如下所示的字段,但是Policy Number字段为空,并且给我一个错误,因为查询不包括特定表达式作为聚合函数的一部分。
IIf((Format([Policy Create Date],"mm/dd/yyyy"))<"11/1/2019",[Customer ID],[Policy Number])
SELECT
[Customer ID],
IIf(IsNull([Policy Number]),'',Trim(Replace(UCase([Policy Number]),Chr(160)," "))) AS [Policy#], Min([Policy Create Date]) AS [Policy Create Date],
[Policy Carrier Plan],
IIf((Format([Policy Create Date],"mm/dd/yyyy"))<"11/1/2019",[Customer ID],[Policy Number]) AS [AUX-FIELD]
FROM [Agent Cubed (Same Plan)]
GROUP BY
[Customer ID],
IIf(IsNull([Policy Number]),'',Trim(Replace(UCase([Policy Number]),Chr(160)," "))),
[Policy Carrier Plan], IIf((Format([Policy Create Date],"mm/dd/yyyy"))<"11/1/2019",
[Customer ID],
[Policy Number])
未聚合的那些columns
应该成为您的group by
子句的一部分。
SELECT [Customer ID]
, IIf(IsNull([Policy Number]),'',Trim(Replace(UCase([Policy Number]),Chr(160)," "))) AS [Policy#]
, Min([Policy Create Date]) AS [Policy Create Date]
, [Policy Carrier Plan]
, [Agent of Record 1 User Name]
, [Agent Cubed Users].[User Key 02]
, IIf((Format([Policy Create Date],"mm/dd/yyyy"))<"11/1/2019", [Customer ID], [Policy Number]) AS [AUX-FIELD]
, DatePart("yyyy",[Policy Create Date]) & DatePart("q",[Policy Create Date]) AS YearQ
FROM [Agent Cubed (Same Plan)]
GROUP BY [Customer ID]
, IIf(IsNull([Policy Number]),'',Trim(Replace(UCase([Policy Number]),Chr(160)," ")))
, [Policy Carrier Plan]
, [Agent of Record 1 User Name]
, [Agent Cubed Users].[User Key 02]
, IIf((Format([Policy Create Date],"mm/dd/yyyy"))<"11/1/2019", [Customer ID], [Policy Number])
, DatePart("yyyy",[Policy Create Date]) & DatePart("q",[Policy Create Date])
HAVING (((DatePart("yyyy",[Policy Create Date]) & DatePart("q",[Policy Create Date]))=4))