【发布时间】:2016-01-26 21:34:27
【问题描述】:
我尝试在以下查询中按 [DueDate] 分组,但该日期可能因设置而异。无论如何我可以完成这项工作吗?
SELECT
(
SELECT CASE
WHEN (SELECT TOP 1 [SiconCFMSetting].[SettingValue] FROM [SiconCFMSetting] WHERE [SiconCFMSetting].[SettingName]='UseAverageTimeToPay') = 'True'
THEN ISNULL([SiconCFMForecastDate].[ForecastDate],DATEADD([DD],[SiconCFMSLCustomerAverageTimeToPayView].[Days],[SLPostedCustomerTran].[TransactionDate]))
ELSE ISNULL([SiconCFMForecastDate].[ForecastDate],[SLPostedCustomerTran].[DueDate])
END
) AS [DueDate],
SUM([SLPostedCustomerTran].[SalControlValueInBaseCurrency]) AS [Value]
FROM [SLPostedCustomerTran]
INNER JOIN [SLCustomerAccount]
ON [SLCustomerAccount].[SLCustomerAccountID]
= [SLPostedCustomerTran].[SLCustomerAccountID]
INNER JOIN [SiconCFMSLCustomerAverageTimeToPayView]
ON [SiconCFMSLCustomerAverageTimeToPayView].[SLCustomerAccountID]
= [SLCustomerAccount].[SLCustomerAccountID]
LEFT JOIN [SiconCFMForecastDate]
ON [SiconCFMForecastDate].[ForecastDateForeignID]
= [SLPostedCustomerTran].[SLPostedCustomerTranID]
AND [SiconCFMForecastDate].[Deleted]=0
AND [SiconCFMForecastDate].[ForecastDateSource]='SLPostedCustomerTran'
WHERE ([SLPostedCustomerTran].[SYSTraderTranTypeID]=4 OR [SLPostedCustomerTran].[SYSTraderTranTypeID]=5)
AND [SLPostedCustomerTran].[SalControlValueInBaseCurrency] <> 0
GROUP BY
CASE
WHEN (SELECT TOP 1 [SiconCFMSetting].[SettingValue] FROM [SiconCFMSetting] WHERE [SiconCFMSetting].[SettingName]='UseAverageTimeToPay') = 'True'
THEN ISNULL([SiconCFMForecastDate].[ForecastDate],DATEADD([DD],[SiconCFMSLCustomerAverageTimeToPayView].[Days],[SLPostedCustomerTran].[TransactionDate]))
ELSE ISNULL([SiconCFMForecastDate].[ForecastDate],[SLPostedCustomerTran].[DueDate])
END
错误: 消息 144,第 15 级,状态 1,第 26 行 不能在用于 GROUP BY 子句的 group by 列表的表达式中使用聚合或子查询。
【问题讨论】:
标签: sql-server