在sql中乘以2列并使用SQL汇总所有结果

时间:2011-07-22 09:13:29

标签: sql-server-2008

我写了一个查询来乘以colums现在我想总结一下我得到的结果可以任何人给我一个想法

这就是我写的

select Rate,Qty,(Rate*Qty) as result from tblName

我会得到结果例如40 90现在我想总结这些结果

2 个答案:

答案 0 :(得分:8)

原始答案

select Sum(Rate) as Rate, Sum(Qty) as Qty, Sum(Rate*Qty) as Result from tblName

编辑 - 试试这个..

select 
     0 as TotalRow, 
     Rate,
     Qty,
    (Rate*Qty) as Result 
from tblName

UNION ALL

select 
     1 as TotalRow,
     Sum(Rate) as Rate, 
     Sum(Qty) as Qty, 
     Sum(Rate*Qty) as Result 
 from tblName

Order By TotalRow

答案 1 :(得分:3)

SELECT SUM(Rate*Qty) as Result
FROM tblName