在SQL计算中使用别名

时间:2019-02-11 19:02:50

标签: sql sql-server aggregate-functions aliases

我有一列称为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语句中,则表明我不能在聚合中包含聚合。有谁知道该怎么办?

这是我尝试过的其他事情,

尝试将行号语句放入Case语句

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

那是行不通的,因为我收到了错误消息“无法在另一个窗口函数或集合的上下文中使用窗口函数”。

我只是尝试在别名周围放置一个选择包装器(正如该答案Using an Alias in SQL Calculations中提到的那样,我原来的问题被标记为重复)

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

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

有人知道如何解决这个问题吗?

这是我的完整查询,以防万一:

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

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

        ,SUM(CASE WHEN (SELECT 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)

ORDER BY Total_Items_Returned_Number desc, Total_Items_Returned_NumberColor desc 

0 个答案:

没有答案