我試圖獲得計算列的總和並顯示爲單獨的不同行(ItemCode),但沒有成功。我愛上我靠近的解決方案,但不知何故,我再次SQL查詢SUM列的乘積列的總和
堅持我的查詢:
select
I.Itemcode,
Cast((POL.Receivedqty) as Int) as QTY,
Cast(SUM(POL.Receivedqty * POL.RW_CostPrice) as Money) as TotalVolumexBuyPrice,
Cast(SUM(POL.ReceivedQty * POL.ItemPrice) as Money) as TotalVolumexCPROSellPrice,
Cast(POL.RW_CostPrice as Money) as LatestCostPrice,
Cast(POL.ItemPrice as money) as LatestSellPrice,
Convert(Varchar, max(POL.Completedate), 111) as LastOrderDate
From
initem as I
left join
InpurchaseOrderLine as POL on I.ItemID = POL.ItemID
where
POL.CompleteDate between '2014-10-01' and '2015-10-01'
and I.Itemcode not like '1000015697'
and I.Itemcode like '1000001453' or I.Itemcode like '1000019133'
Group by
I.ItemCode, POL.ItemPrice, POL.RW_CostPrice, POL.Receivedqty
Order by
POL.RW_CostPrice
這將返回這樣的數據:的
ItemCode QTY Cost x QTY Sell x QTY CostPrice SellPrice
1000001453 0 0.00 0.00 794.00 941.37
1000001453 14 11116.00 13179.18 794.00 941.37
1000001453 15 11910.00 14120.55 794.00 941.37
1000001453 20 31760.00 37654.80 794.00 941.37
1000001453 14 25592.00 26358.36 914.00 941.37
1000001453 20 73120.00 75309.60 914.00 941.37
1000001453 30 27420.00 28241.10 914.00 941.37
1000001453 31 28334.00 29182.47 914.00 941.37
1000019133 1 39781.90 45232.02 3978.19 4523.202
1000019133 2 7956.38 9046.404 3978.19 4523.202
1000019133 1 3978.19 4523.2022 3978.19 4523.2022
1000019133 0 0.00 0.00 3978.19 4523.21
1000019133 1 43760.09 49755.31 3978.19 4523.21
1000019133 2 7956.38 9046.4 3978.19 4523.21
1000019133 2 7956.38 9408.2658 3978.19 4704.1329
1000019133 2 8274.64 9408.2658 4137.32 4704.1329
但我想獲得類似的東西(總和款項):
ItemCode QTY Cost x QTY Sell x QTY
1000001453 238 209252.00 224046.06
1000001933 11 119661.00 136418.00
感謝Brino,做了這份工作。 – Brunoxp