2015-10-28 47 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