Anatoly Lubarsky Logo
MSSQL, .NET, Design. Life and Music

T-SQL: Aggregate Values Multiplication Like SUM Aggregates Values Sum

Ever thought how to select multiplication from the database table the same way like ANSI sum:


select sum(myfield) 
from mytable

The following query shows how to do it. It uses one small mathematical trick that:


 
log(A * B) = log(A) + log(B)

Check it out !


 
select cast(exp(sum(log(my_field))) as int) as myresult 
from mytable

Related Posts:

Sunday, November 30, 2003 1:36 AM

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)