Vivekh
Vivekh

Reputation: 4259

Multiply 2 columns in sql and to sum all the results using SQL

I write a query to multiply to colums now i would like to sum up the result that i am getting can any one give me an idea

this is what i wrote

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

I will get result say for example 40 90 now i would like to sum these results

Upvotes: 8

Views: 65355

Answers (2)

CResults
CResults

Reputation: 5105

Original Answer

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

EDIT - Try this..

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

Upvotes: 12

Curtis
Curtis

Reputation: 103348

SELECT SUM(Rate*Qty) as Result
FROM tblName

Upvotes: 10

Related Questions