减去查询中的值

时间:2013-08-27 13:36:25

标签: sql sql-server

您好我正在运行下面的查询,我的[sumofnetsales]金额为空值。有谁知道可能是什么问题?

另外,如果我想使用[SumofAmountShipped]列而不是在减法方程中再次求和,我该怎么做?谢谢。

DECLARE @Rundate datetime
SET @RunDate = '3/11/2013' -- Date they run the report 


--Temp Tables to sum up all accural types

Declare @Chargeback table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @Chargeback
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 2
Group By
    ShortItemNo,
    ProductName


Declare @AdjustedForNetPrice table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @AdjustedForNetPrice
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 2
Group By
    ShortItemNo,
    ProductName


Declare @AdminFee table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @AdminFee
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 1
Group By
    ShortItemNo,
    ProductName


Declare @Returns table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @Returns
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 3
Group By
    ShortItemNo,
    ProductName


Declare @Rebates table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @Rebates
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 4
Group By
    ShortItemNo,
    ProductName


Declare @ACCPSW1 table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @ACCPSW1
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 5
Group By
    ShortItemNo,
    ProductName


Declare @CashDiscount table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @CashDiscount
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 6
Group By
    ShortItemNo,
    ProductName


Declare @INIT1 table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @INIT1 
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 7
Group By
    ShortItemNo,
    ProductName


Declare @Medicaid table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @Medicaid 
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 8
Group By
    ShortItemNo,
    ProductName


Declare @InitialOrderDiscount table (Amount money, ShortItemNo int, ProductName varchar(50))
INSERT @InitialOrderDiscount
SELECT sum(a.AccrualAmount),
       ShortItemNo,
       ProductName
FROM 
    Accruals a
WHERE 
    @RunDate between a.AccrualBeginDate and a.AccrualEndDate and a.enddate = '12/31/2200' and accrualtype = 9
Group By
    ShortItemNo,
    ProductName





SELECT
    rtrim(IMDSC1) [ItemDesc1],
    rtrim(IMDSC2) [ItemDesc2],
    sum(QuantityShipped) [SumOfQuantityShipped],
    sum(ExtendedPrice) [SumOfAmountShipped],
    rtrim(IMSRTX) [BrandName],
    '' [SumOfNetSaleUnitPrice],
    sum(ExtendedPrice) - c.Amount - a.Amount - af.Amount - r.Amount - rr.Amount - ac.Amount - cd.Amount - i.Amount - m.Amount - id.Amount [SumOfNetSales],
    '' [SumOfGrossProfit],
    '' [SumOfGrossMargin],
    '3/11/2013' [Rundate]
FROM
    SalesSummary ss join [Product] p 
        on ss.ShortItemNo = p.SDITM
    join JDE_PRODUCTION.PRODDTA.F4101 im 
        on im.IMITM = p.SDITM
    left join @Chargeback c 
        on c.ShortItemNo = ss.ShortItemNo
    left join @AdjustedForNetPrice a
        on a.ShortItemNo = ss.ShortItemNo
    left join @AdminFee af
        on af.ShortItemNo = ss.ShortItemNo
    left join @Returns r 
        on r.ShortItemNo = ss.ShortItemNo
    join @Rebates rr
        on rr.ShortItemNo = ss.ShortItemNo
    left join @ACCPSW1 ac
        on ac.ShortItemNo = ss.ShortItemNo
    left join @CashDiscount cd
        on cd.ShortItemNo = ss.ShortItemNo
    left join @INIT1 i
        on i.ShortItemNo = ss.ShortItemNo
    left join @Medicaid m
        on m.ShortItemNo = ss.ShortItemNo
    left join @InitialOrderDiscount id
        on id.ShortItemNo = ss.ShortItemNo      

WHERE 
    ss.InvoiceDate = @RunDate

GROUP BY
    rtrim(IMDSC1),
    rtrim(IMDSC2),
    rtrim(IMSRTX),
    c.Amount,
    a.Amount,
    af.Amount,
    r.Amount,
    rr.Amount,
    ac.Amount,
    cd.Amount,
    i.Amount,
    m.Amount,
    id.Amount
ORDER BY 
    rtrim(IMDSC1),
    rtrim(IMDSC2) 

3 个答案:

答案 0 :(得分:2)

您收到null,因为减法中的一个列值为null。就像:

1000 - null = null

您必须使用coalesce(<column>, 0)(= ANSI SQL方式)来避免这种情况,或使用ISNULL()函数(特定于SQL Server)。你的减法线看起来像:

sum(coalesce(ExtendedPrice, 0)) - coalesce(c.Amount,0) - coalesce(a.Amount, 0) 
    - coalesce(af.Amount, 0) - ... and so on ... [SumOfNetSales]

从第二点开始:你不能避免在减法中写出[SumofAmountShipped]列,因为你不能在同一个语句中使用刚刚定义的别名。

答案 1 :(得分:0)

稍微难以理解您的代码,但问题很可能在于JOIN。

当使用左连接时,如果仅存在左侧,则它将返回右侧的左侧+ NULL。 如果您执行任何涉及NULL的算术,结果将始终为NULL。

要解决此问题,请使用ISNULL函数为减法中的每个值返回0而不是NULL。

祝你好运!

答案 2 :(得分:0)

您获得[sumofnetsales]的空值,因为您在表@InitialOrderDiscount(左连接@InitialOrderDiscount id on id.ShortItemNo = ss.ShortItemNo )中没有匹配值。因此,为了避免使用NULL,您可以使用以下列:

ISNULL(id.Amount,0)[SumOfNetSales]