2015-10-28 48 views
0

我試圖獲得計算列的總和並顯示爲單獨的不同行(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 

回答

0

你被I.ItemCode,POL.ItemPrice,POL.RW_CostPrice,POL.Receivedqty,當你只需要按I.ItemCode

集團通過I.ItemCode,然後用aggregate functions總結所有的分組相應的採購訂單行。

select 
    I.Itemcode, 
    Cast(Sum(POL.Receivedqty) as Int) as QTY, 
    Cast(SUM(POL.Receivedqty * POL.RW_CostPrice) as Money) as "Cost x QTY", 
    Cast(SUM(POL.ReceivedQty * POL.ItemPrice) as Money) as "Sell x QTY" 
From 
    initem as I 
left join 
    InpurchaseOrderLine as POL on POL.ItemID = I.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 
+0

感謝Brino,做了這份工作。 – Brunoxp

0
WITH cte AS 
(
    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 
) 
SELECT Itemcode, 
    QTY = SUM(Qty), 
    [Cost x QTY] = SUM(TotalVolumexBuyPrice) 
    [Cost x QTY] = SUM(TotalVolumexCPROSellPrice) 
FROM cte 
GROUP BY Itemcode 
ORDER BY Itemcode; 
+0

lad2025不幸的是,「With」沒有正常工作,因爲結果告訴我: – Brunoxp