こんにちは、以下のクエリを実行していますが、[sumofnetsales] の金額に対して null 値を取得しています。何が問題なのか誰か知っていますか?
また、減算式で再度合計する代わりに [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)