如何将两列相乘并将结果存储在单列中,然后生成新列的总和?

时间:2013-09-29 07:02:30

标签: sql-server sql-server-2008 tsql

我有两列价格和数量。我把它们相乘了

select Price*Quantity as SubTotal from ProductInfo 

现在我想将小计中所有行的总和作为总计

SUM(SubTotal) as GrandTotal

和 因此像这样的新查询

select Price*Quantity as SubTotal , SUM(SubTotal) as GrandTotal from ProductInfo 

我该怎么做才能帮忙

2 个答案:

答案 0 :(得分:0)

试试这个::

Select Price*Quantity as SubTotal, 
SUM(Price*Quantity) as GrandTotal 
from ProductInfo 

这将按预期结果。

答案 1 :(得分:0)

Select ISNULL(Price*Quantity, 'GrandTotal') as SubTotal, 
SUM(Price*Quantity) as Total 
from ProductInfo
GROUP BY   ISNULL(Price*Quantity, 'GrandTotal')
WITH ROLLUP