Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL中子查询的和_Sql_Sql Server_Tsql - Fatal编程技术网

SQL中子查询的和

SQL中子查询的和,sql,sql-server,tsql,Sql,Sql Server,Tsql,我试图对一列进行求和,但似乎出现了以下错误: 无法对包含聚合或子查询的表达式执行聚合函数 当前正在运行的查询: SELECT A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup], [FiscalYear], (SELECT Sum(Amount) WHERE p.PromotionStatusId = 0) AS Actual, (SELECT Sum(Amount) WHERE p.Promotio

我试图对一列进行求和,但似乎出现了以下错误:

无法对包含聚合或子查询的表达式执行聚合函数

当前正在运行的查询:

SELECT A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup], [FiscalYear],
    (SELECT Sum(Amount)
    WHERE   p.PromotionStatusId = 0) AS Actual,
    (SELECT Sum(Amount)
    WHERE   p.PromotionStatusId = 2) AS Committed,
    (SELECT Sum(Amount)
    WHERE   p.PromotionStatusId = 1 OR p.PromotionStatusId = 1002) AS Planned
FROM [Promotions] P
JOIN [PromotionAdSpends] A ON P.Id = A.PromotionId
JOIN [opt_PromotionSpendTypes] PS ON PS.Id = A.AdSpendPromotionSpendTypeId
GROUP BY A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup],
         [FiscalYear], PromotionStatusId
输出:

由于第1行和第2行具有相同的详细信息,我想将它们分组并将列的总和相加

当我将查询更改为下面的时,它会抛出错误

无法对包含聚合或子查询的表达式执行聚合函数

不知道我错过了什么

SELECT DISTINCT A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup], [FiscalYear],
    SUM((SELECT Sum(Amount)
    WHERE   p.PromotionStatusId = 0)) AS Actual,
    SUM((SELECT Sum(Amount)
    WHERE   p.PromotionStatusId = 2)) AS Committed,
    SUM((SELECT Sum(Amount)
    WHERE   p.PromotionStatusId = 1 OR p.PromotionStatusId = 1002)) AS Planned
FROM [Promotions] P
JOIN [PromotionAdSpends] A ON P.Id = A.PromotionId
JOIN [opt_PromotionSpendTypes] PS ON PS.Id = A.AdSpendPromotionSpendTypeId
GROUP BY A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup],
         [FiscalYear], PromotionStatusId

使用条件聚合:

SELECT A.AdSpendPromotionSpendTypeId, PS.Name, [MajorGroup], [FiscalYear],
       Sum(CASE WHEN p.PromotionStatusId = 0 THEN Amount END) AS Actual,
       Sum(CASE WHEN p.PromotionStatusId = 2 THEN Amount END) AS Committed,
       Sum(CASE WHEN p.PromotionStatusId IN 1, 2) THEN Amount END) AS Planned
FROM [Promotions] P JOIN
     [PromotionAdSpends] A ON P.Id = A.PromotionId JOIN
     [opt_PromotionSpendTypes] PS ON PS.Id = A.AdSpendPromotionSpendTypeId
GROUP BY A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup], [FiscalYear]

使用条件聚合:

SELECT A.AdSpendPromotionSpendTypeId, PS.Name, [MajorGroup], [FiscalYear],
       Sum(CASE WHEN p.PromotionStatusId = 0 THEN Amount END) AS Actual,
       Sum(CASE WHEN p.PromotionStatusId = 2 THEN Amount END) AS Committed,
       Sum(CASE WHEN p.PromotionStatusId IN 1, 2) THEN Amount END) AS Planned
FROM [Promotions] P JOIN
     [PromotionAdSpends] A ON P.Id = A.PromotionId JOIN
     [opt_PromotionSpendTypes] PS ON PS.Id = A.AdSpendPromotionSpendTypeId
GROUP BY A.AdSpendPromotionSpendTypeId , PS.Name, [MajorGroup], [FiscalYear]

从分组依据中删除PromotionStatusId。很少从不需要在分组依据时执行SELECT DISTINCT。从分组依据中删除PromotionStatusId。很少从不需要在分组依据时执行SELECT DISTINCT。