SQL查询SUM的乘法列的SUM

时间:2015-10-28 14:36:18

标签: sql-server-2008

我正在尝试获取计算列的总和并显示为单独的不同行(ItemCode)但没有成功。我摔倒了,我接近解决方案,但不知怎的,我再次陷入困境

我的查询:

TIME

返回此数据:

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

但我想得到类似的东西(Sum of Sums):

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       

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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