Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 671
Vivekh Avatar asked Jul 22 '11 09:07

Vivekh


1 Answers

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
like image 185
CResults Avatar answered Jan 03 '23 19:01

CResults