SQL无效的列名称和行号

时间:2019-02-11 17:23:11

标签: sql sql-server aggregate-functions

我的查询有点长,所以我将尝试使内容简短,只讨论相关信息。我有一列称为Total_Returned_Value。我想对所有单个返回值求和以获得所有返回的最终总返回值。由于表的结构,该列具有重复项,因此我不能仅对该列进行求和。我想根据另一个名为“ Customer_Purchase_Order_Number”的“ id”类型列对该列求和,其中仅对每个采购订单号之一求和。

这就是我的做法。

,rn = ROW_NUMBER() OVER (PARTITION BY Customer_Purchase_Order_Number ORDER BY Customer_Purchase_Order_Number)

,SUM(CASE WHEN rn = 1 then rh.Total_Returned_Value ELSE 0 END) OVER (PARTITION BY rh.Customer_Number) as Total_Returned_Values 

因此,它按Customer_Purchase_Order_Number进行组织,为每组相等的数字分配一个行号,并且仅将行号= 1的那些相加。

似乎很简单。但是,出现错误“无效的列名'rn'。”。如果我尝试将行号选择语句直接放入case语句中,则表明我不能在聚合中包含聚合。有谁知道该怎么办?

这是我的完整代码。

DECLARE @LocalCompanyCode VARCHAR(5)
SET @LocalCompanyCode = '03'
DECLARE @LocalDivisionCode VARCHAR(5)
SET @LocalDivisionCode = '001'
DECLARE @LocalFROMDate DATETIME
SET @LocalFROMDate = '2/04/2019'
DECLARE @LocalToDate DATETIME
SET @LocalToDate = '2/08/2019'
SET NOCOUNT ON;

WITH rh AS

(SELECT  rh.Company_Code
        ,rh.Division_Code
        ,rh.Date_Created
        ,rh.Customer_Number 
        ,rh.Customer_Purchase_Order_Number
        ,rh.Return_Number
        ,(rh.Total_Value-rh.Freight_Charges) as Returned_Value
        ,rh.Freight_Charges
        ,rh.Remarks
        ,SUM(rh.Total_Value-rh.Freight_Charges) 
            OVER (PARTITION BY rh.Customer_Purchase_Order_Number) as Total_Returned_Value

FROM [JMNYC-AMTDB].[AMTPLUS].[dbo].Returns_Header rh (nolock)

)

SELECT   rh.Company_Code
        ,rh.Division_Code
        ,rh.Date_Created
        ,rh.Customer_Number  
        ,rh.Customer_Purchase_Order_Number
        ,rh.Return_Number
        ,rd.Item_Number
        ,Sum(rd.Quantity_Returned) OVER (PARTITION BY rd.Item_Number) as Total_Items_Returned_Number
        ,rd.Color_Code
        ,Sum(rd.Quantity_Returned) OVER (PARTITION BY rd.Item_Number, rd.Color_Code) as Total_Items_Returned_NumberColor
        ,rd.Quantity_Returned
        ,rh.Returned_Value
        ,rh.Freight_Charges
        ,rh.Remarks
        ,rh.Total_Returned_Value

        /*,rn = ROW_NUMBER() OVER (PARTITION BY Customer_Purchase_Order_Number ORDER BY Customer_Purchase_Order_Number)

        ,SUM(CASE WHEN rn = 1 then rh.Total_Returned_Value ELSE 0 END) OVER (PARTITION BY rh.Customer_Number) as Total_Returned_Values*/

        ,DENSE_RANK() OVER (PARTITION BY rh.Customer_Number ORDER BY rh.Customer_Purchase_Order_Number ASC) 
            + DENSE_RANK() OVER (PARTITION BY rh.Customer_Number ORDER BY rh.Customer_Purchase_Order_Number DESC) 
                - 1 AS Total_Returns_Count

        ,SUM(CASE WHEN rh.Remarks LIKE '%lost%' then 1 ELSE 0 END) OVER (PARTITION BY rh.Customer_Number) as "Lost_Packages"

        ,SUM (Quantity_Returned) OVER () as "Total_ReturnedItems_Count" 

FROM rh

LEFT JOIN

[JMNYC-AMTDB].[AMTPLUS].[dbo].Returns_Detail rd (nolock) 
    on rd.Return_Number = rh.Return_number

WHERE 
    (rh.Company_Code = @LocalCompanyCode OR @LocalCompanyCode IS NULL) AND 
    (rh.Division_Code = @LocalDivisionCode OR @LocalDivisionCode IS NULL) AND
    rh.Customer_Number = 'ecom2x' AND 
    rh.Customer_Purchase_Order_Number not like '%bulk%' AND
    rh.Date_Created BETWEEN @LocalFROMDate AND DATEADD(dayofyear, 1, @LocalToDate)
    --AND rh.remarks like '%magic%'

ORDER BY Total_Items_Returned_Number desc, Total_Items_Returned_NumberColor desc 

1 个答案:

答案 0 :(得分:0)

我已解决您的查询。您不能将RN用作别名列名称,因此请完全替换rn,它将起作用

DECLARE @LocalCompanyCode VARCHAR(5)
SET @LocalCompanyCode = '03'
DECLARE @LocalDivisionCode VARCHAR(5)
SET @LocalDivisionCode = '001'
DECLARE @LocalFROMDate DATETIME
SET @LocalFROMDate = '2/04/2019'
DECLARE @LocalToDate DATETIME
SET @LocalToDate = '2/08/2019'
SET NOCOUNT ON;

WITH rh AS

(SELECT  rh.Company_Code
        ,rh.Division_Code
        ,rh.Date_Created
        ,rh.Customer_Number 
        ,rh.Customer_Purchase_Order_Number
        ,rh.Return_Number
        ,(rh.Total_Value-rh.Freight_Charges) as Returned_Value
        ,rh.Freight_Charges
        ,rh.Remarks
        ,SUM(rh.Total_Value-rh.Freight_Charges) 
            OVER (PARTITION BY rh.Customer_Purchase_Order_Number) as Total_Returned_Value

FROM [JMNYC-AMTDB].[AMTPLUS].[dbo].Returns_Header rh (nolock)

)

SELECT   rh.Company_Code
        ,rh.Division_Code
        ,rh.Date_Created
        ,rh.Customer_Number  
        ,rh.Customer_Purchase_Order_Number
        ,rh.Return_Number
        ,rd.Item_Number
        ,Sum(rd.Quantity_Returned) OVER (PARTITION BY rd.Item_Number) as Total_Items_Returned_Number
        ,rd.Color_Code
        ,Sum(rd.Quantity_Returned) OVER (PARTITION BY rd.Item_Number, rd.Color_Code) as Total_Items_Returned_NumberColor
        ,rd.Quantity_Returned
        ,rh.Returned_Value
        ,rh.Freight_Charges
        ,rh.Remarks
        ,rh.Total_Returned_Value

        ,rn = ROW_NUMBER() OVER (PARTITION BY Customer_Purchase_Order_Number ORDER BY Customer_Purchase_Order_Number)

        ,SUM(CASE WHEN ROW_NUMBER() OVER (PARTITION BY Customer_Purchase_Order_Number ORDER BY Customer_Purchase_Order_Number) = 1 then rh.Total_Returned_Value ELSE 0 END) OVER (PARTITION BY rh.Customer_Number) as Total_Returned_Values

        ,DENSE_RANK() OVER (PARTITION BY rh.Customer_Number ORDER BY rh.Customer_Purchase_Order_Number ASC) 
            + DENSE_RANK() OVER (PARTITION BY rh.Customer_Number ORDER BY rh.Customer_Purchase_Order_Number DESC) 
                - 1 AS Total_Returns_Count

        ,SUM(CASE WHEN rh.Remarks LIKE '%lost%' then 1 ELSE 0 END) OVER (PARTITION BY rh.Customer_Number) as "Lost_Packages"

        ,SUM (Quantity_Returned) OVER () as "Total_ReturnedItems_Count" 

FROM rh

LEFT JOIN

[JMNYC-AMTDB].[AMTPLUS].[dbo].Returns_Detail rd (nolock) 
    on rd.Return_Number = rh.Return_number

WHERE 
    (rh.Company_Code = @LocalCompanyCode OR @LocalCompanyCode IS NULL) AND 
    (rh.Division_Code = @LocalDivisionCode OR @LocalDivisionCode IS NULL) AND
    rh.Customer_Number = 'ecom2x' AND 
    rh.Customer_Purchase_Order_Number not like '%bulk%' AND
    rh.Date_Created BETWEEN @LocalFROMDate AND DATEADD(dayofyear, 1, @LocalToDate)
    --AND rh.remarks like '%magic%'

ORDER BY Total_Items_Returned_Number desc, Total_Items_Returned_NumberColor desc