Sql server SQL-不能在Group子句中使用子查询的聚合

Sql server SQL-不能在Group子句中使用子查询的聚合,sql-server,Sql Server,我试图在下面的查询中按[DueDate]分组,但根据设置,该日期可能会有所不同。有什么办法可以让这一切顺利吗 SELECT ( SELECT CASE WHEN (SELECT TOP 1 [SiconCFMSetting].[SettingValue] FROM [SiconCFMSetting] WHERE [SiconCFMSetting].[SettingName]='UseAverageTimeToPay') = 'True' THEN ISNULL([Sic

我试图在下面的查询中按[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
选择
(
选择案例
当(从[SiconCFMSetting]中选择顶部1[SiconCFMSetting]。[SettingValue],其中[SiconCFMSetting]。[SettingName]='UseAverageTimeToPay')='True'
然后为空([SiconCFMForecastDate].[ForecastDate],DATEADD([DD],[SiconCFMSLCustomerAverageTimeToPayView]。[Days],[SLPostedCustomerTran]。[TransactionDate]))
ELSE为空([SiconCFMForecastDate].[ForecastDate],[SLPostedCustomerTran].[DueDate])
结束
)作为[到期日],
总和([SLPostedCustomerTran].[SalControlValueInBaseCurrency])为[Value]
来自[SLPostedCustomerTran]
内部联接[SLCustomerAccount]
在[SLCustomerAccount]。[SLCustomerAccount]
=[SLPostedCustomerTran]。[SLCustomerAccountID]
内部联接[SICONCFMLCustomerAverageTimeToPayView]
在[SICONCFMLCustomerAverageTimeToPayView]。[SLCustomerAccountID]
=[SLCustomerAccount]。[SLCustomerAccount]
左连接[SiconCFMForecastDate]
在[SiconCFMForecastDate]。[ForecastDateForeigned]
=[SLPostedCustomerTran]。[SLPostedCustomerTranID]
并且[SiconCFMForecastDate].[Deleted]=0
和[SiconCFMForecastDate]。[ForecastDateSource]=“SLPostedCustomerTran”
其中([SLPostedCustomerTran].[SYSTraderTranTypeID]=4或[SLPostedCustomerTran].[SYSTraderTranTypeID]=5)
和[SLPostedCustomerTran]。[SalControlValueInBaseCurrency]0
分组
案例
当(从[SiconCFMSetting]中选择顶部1[SiconCFMSetting]。[SettingValue],其中[SiconCFMSetting]。[SettingName]='UseAverageTimeToPay')='True'
然后为空([SiconCFMForecastDate].[ForecastDate],DATEADD([DD],[SiconCFMSLCustomerAverageTimeToPayView]。[Days],[SLPostedCustomerTran]。[TransactionDate]))
ELSE为空([SiconCFMForecastDate].[ForecastDate],[SLPostedCustomerTran].[DueDate])
结束
错误: 味精144,15级,状态1,第26行
无法在用于group by子句的group by列表的表达式中使用聚合或子查询。

在外部查询中进行聚合。你可以这样做

with cte as
(
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],
[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
)
select DueDate,sum(Value)
from cte 
group by DueDate

谢谢,我现在就试一试!这就是诀窍!,我会尽快接受答案me@WraithNath检查另一种方式谢谢,我用一个外部选择做了一些测试,你用的语句和这两个都是非常相似的计时。外部应用会更快吗?是什么让你也这么想的?
SELECT OA.[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'
       OUTER apply (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) OA ([DueDate])
WHERE  ( [SLPostedCustomerTran].[SYSTraderTranTypeID] = 4
          OR [SLPostedCustomerTran].[SYSTraderTranTypeID] = 5 )
       AND [SLPostedCustomerTran].[SalControlValueInBaseCurrency] <> 0