購入したアイテムの量とそのコストを12、24、36か月間隔でエージングするエージングクエリがあります。クエリは問題なく実行されますが、1つの特定のサプライヤの合計を見つけるために使用される単一のクエリに対して、私の合計は合計されません。
下記参照。
SELECT s.SupplierNumber as Supplier_Number
, s.suppliername as Supplier_Name
, i.supplierpartnum as Part_No
, SUBSTRING(e.account_code,1,5)/*+'-'+RIGHT(e.account_code,7)*/ as Account_Code
, CASE WHEN e.reference_code = ''
THEN 'NOREF'
ELSE reference_code
END as Reference_Code
, i.Commodity
, i.ShortDscrptn as Part_Desc
, i.unitofmeasure as UOM
, i.unitprice as Unit_Price
, CASE WHEN h.OrderDate >= '2012-01-01'
AND h.OrderDate <= '2012-12-31'
THEN COUNT(i.Quantity)
ELSE 0 END as Annual_Qty_12
, CASE WHEN h.OrderDate >= '2011-01-01'
AND h.OrderDate <= '2012-12-31'
THEN COUNT(i.Quantity)
ELSE 0 END as Annual_Qty_24
, CASE WHEN h.OrderDate >= '2010-01-01'
AND h.OrderDate <= '2012-12-31'
THEN COUNT(i.Quantity)
ELSE 0 END as Annual_Qty_36
, CASE WHEN h.OrderDate >= '2012-01-01'
AND h.OrderDate <= '2012-12-31'
THEN SUM(i.UnitPrice)
ELSE 0 END as Annual_Spend_12
, CASE WHEN h.OrderDate >= '2011-01-01'
AND h.OrderDate <= '2012-12-31'
THEN SUM(i.UnitPrice)
ELSE 0 END as Annual_Spend_24
, CASE WHEN h.OrderDate >= '2010-01-01'
AND h.OrderDate <= '2012-12-31'
THEN SUM(i.UnitPrice)
ELSE 0 END as Annual_Spend_36
FROM ekp.dbo.SUPPLIER s
JOIN ekp.dbo.ORDERHEADER h
ON s.SupplierID = h.SupplierID
JOIN ekp.dbo.ORDERITEM i
ON h.OrderID = i.OrderID
JOIN mgnt.dbo.ematch e
ON h.PONumber = e.po_ctrl_num
AND i.LineNumber = e.po_sequence_id
WHERE h.OrderDate >= '2010-01-01'
AND h.OrderDate <= '2012-12-31'
GROUP BY s.SupplierNumber, s.SupplierName, i.SupplierPartNum, e.account_code
, i.Commodity, i.UnitOfMeasure, i.UnitPrice, i.ShortDscrptn, h.OrderDate
, e.reference_code
Annual_Spend_12
先に進んで結果をスプレッドシートにコピーし、特定の列を合計するとSupplier
、次のように一致しません。
SELECT SUM(i.unitprice) as Annual_Spend_12
FROM ekp.dbo.ORDERITEM i
JOIN ekp.dbo.ORDERHEADER h
ON i.OrderID = h.OrderID
JOIN ekp.dbo.Supplier s
ON h.SupplierID = s.SupplierID
JOIN mgnt.dbo.ematch e
ON h.PONumber = e.po_ctrl_num
AND i.LineNumber = e.po_sequence_id
WHERE s.SupplierNumber = '15302'
AND h.OrderDate >= '2012-01-01'
AND h.OrderDate <= '2012-12-31'
すぐ上のこのクエリは、常に合計がはるかに高く、一番上のクエリよりもはるかに高くなります。また、代わりに集計全体を合計してカウントしましたが、これも一致しません。実際には、上位のクエリと同じ結果が生成されます。
, COUNT(CASE WHEN h.OrderDate >= '2012-01-01'
AND h.OrderDate <= '2012-12-31'
THEN i.Quantity
ELSE 0 END) as Annual_Qty_12
, COUNT(CASE WHEN h.OrderDate >= '2011-01-01'
AND h.OrderDate <= '2012-12-31'
THEN i.Quantity
ELSE 0 END) as Annual_Qty_24
, COUNT(CASE WHEN h.OrderDate >= '2010-01-01'
AND h.OrderDate <= '2012-12-31'
THEN i.Quantity
ELSE 0 END) as Annual_Qty_36
, SUM(CASE WHEN h.OrderDate >= '2012-01-01'
AND h.OrderDate <= '2012-12-31'
THEN i.UnitPrice
ELSE 0 END) as Annual_Spend_12
, SUM(CASE WHEN h.OrderDate >= '2011-01-01'
AND h.OrderDate <= '2012-12-31'
THEN i.UnitPrice
ELSE 0 END) as Annual_Spend_24
, SUM(CASE WHEN h.OrderDate >= '2010-01-01'
AND h.OrderDate <= '2012-12-31'
THEN i.UnitPrice
ELSE 0 END) as Annual_Spend_36
アグリゲートごとに一時テーブルを使用してストアドプロシージャを作成することを真剣に恐れています。
どんな助けでも大歓迎です。