包含外部引用的聚合表达式

时间:2018-12-11 21:20:12

标签: sql-server

我有这个代码

CREATE PROCEDURE [dbo].[RepSuiviEncours]
    @FirstDate DATE,
    @LastDate DATE
AS
    SELECT 
        proinfo.ProjectN AS 'BS',
        cust.Name AS 'Client',
        proinfo.ProjectName AS 'Projet',
        proinfo.Weight AS 'Poids Dossier Kg',
        SUM(ProShip.[Weight] * [Quantity]) AS 'Poids Feb du Mois',
        (SELECT((ISNULL(prev.PreviousWeight, 0)) + SUM(ProShip.[Weight] * [Quantity])) AS 'Poids Antérieur' 
         FROM [DailyPaintProduction] 
         WHERE Note != 'Premiére couche' 
           AND Note != 'Deuxiéme couche' 
           AND CONVERT(DATE, [CreationDate]) < @FirstDate) 
    FROM 
        [dbo].[DailyPaintProduction] daiPaiPro
    INNER JOIN
        ProjectShipping ProShip ON ProShip.id = [FK_idPartShip]
    INNER JOIN
        ProjectInfo proinfo ON proinfo.id = ProShip.IdProject
    INNER JOIN
        Customer cust ON cust.ID = proinfo.FK_Customer
    LEFT JOIN
        PreviousWeight prev ON prev.FK_Project = proinfo.id
    WHERE 
        Note != 'Premiére couche' 
        AND Note != 'Deuxiéme couche' 
        AND CONVERT(DATE, [CreationDate]) BETWEEN @FirstDate AND @LastDate
    GROUP BY
        proinfo.ProjectN, cust.Name, proinfo.ProjectName,
        PreviousWeight, proinfo.Weight
    ORDER BY
        proinfo.ProjectN

执行此代码时,出现此错误

  

消息8124,级别16,状态1,过程RepSuiviEncours,第10行[Batch Start Line 2]
  在包含外部引用的聚合表达式中指定了多个列。如果要聚合的表达式包含外部引用,则该外部引用必须是表达式中唯一引用的列。

如何解决此问题?预先感谢
我已经更新了代码,看起来像这样

USE [SIM]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[RepSuiviEncours]
@FirstDate DATE,
@LastDate DATE
AS
SELECT 
    proinfo.ProjectN AS 'BS',
    cust.Name AS 'Client',
    proinfo.ProjectName AS 'Projet',
    proinfo.Weight AS 'Poids Dossier Kg',
    SUM(ProShip.[Weight] * [Quantity]) AS 'Poids Feb du Mois',
    (SELECT(SUM(ProShipp.[Weight] * dailypaint.[Quantity])+(ISNULL(prev2.PreviousWeight, 0)))
     FROM [DailyPaintProduction] dailypaint 
     INNER JOIN
    ProjectShipping ProShipp ON ProShipp.id = dailypaint.[FK_idPartShip]
    INNER JOIN
    ProjectInfo proinfo2 ON proinfo2.id = ProShipp.IdProject
    LEFT JOIN
    PreviousWeight prev2 ON prev2.FK_Project = proinfo2.id
     WHERE Note != 'Premiére couche' 
       AND Note != 'Deuxiéme couche' 
       AND CONVERT(DATE, dailypaint.[CreationDate]) < @FirstDate GROUP BY prev2.PreviousWeight)  AS 'Poids Antérieur' 
FROM 
    [dbo].[DailyPaintProduction] daiPaiPro
INNER JOIN
    ProjectShipping ProShip ON ProShip.id = [FK_idPartShip]
INNER JOIN
    ProjectInfo proinfo ON proinfo.id = ProShip.IdProject
INNER JOIN
    Customer cust ON cust.ID = proinfo.FK_Customer
LEFT JOIN
    PreviousWeight prev ON prev.FK_Project = proinfo.id
WHERE 
    Note != 'Premiére couche' 
    AND Note != 'Deuxiéme couche' 
    AND CONVERT(DATE, [CreationDate]) BETWEEN @FirstDate AND @LastDate
GROUP BY
    proinfo.ProjectN, cust.Name, proinfo.ProjectName,
    PreviousWeight, proinfo.Weight
ORDER BY
    proinfo.ProjectN

但是当我执行这段代码时,我又遇到了另一个错误

  

信息512,第16级,状态1,第12行   子查询返回的值超过1。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。   请帮我

0 个答案:

没有答案