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

时间:2016-01-26 12:51:42

标签: sql-server

我尝试在[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

错误: Msg 144,Level 15,State 1,Line 26 不能在用于GROUP BY子句列表的表达式中使用聚合或子查询。

1 个答案:

答案 0 :(得分:2)

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

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

要在单个查询中使用OUTER APPLY

执行此操作
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