dbTalk Databases Forums  

Function like SUM() but multiply

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss Function like SUM() but multiply in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Christoph Boget
 
Posts: n/a

Default Function like SUM() but multiply - 07-09-2007 , 10:50 AM






Is there a function that I can use to multiply all of the values of a column
from a result set together? Much like SUM() adds all the values together?
Or am I going to have to just use AVG() and COUNT() and multiply those 2
values together?

thnx,
Christoph



Reply With Quote
  #2  
Old   
Russell Fields
 
Posts: n/a

Default Re: Function like SUM() but multiply - 07-09-2007 , 11:01 AM






Christoph,

http://sqljunkies.com/WebLog/mosha/a...plication.aspx

RLF

"Christoph Boget" <jcboget (AT) yahoo (DOT) com> wrote

Quote:
Is there a function that I can use to multiply all of the values of a
column from a result set together? Much like SUM() adds all the values
together? Or am I going to have to just use AVG() and COUNT() and multiply
those 2 values together?

thnx,
Christoph




Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Function like SUM() but multiply - 07-09-2007 , 01:46 PM



On Mon, 9 Jul 2007 11:50:24 -0400, Christoph Boget wrote:

Quote:
Is there a function that I can use to multiply all of the values of a column
from a result set together? Much like SUM() adds all the values together?
Or am I going to have to just use AVG() and COUNT() and multiply those 2
values together?

thnx,
Christoph

Hi Christoph,

If you're on SQL Server 2005, you can write a custom aggregate for this.
However, the technique below, which works on all versions of SQL Server,
is probably faster (though not really easy to understand for the novice
coder, and requiring some maths skills to graps as well).

(Simple version - use only if you know for sure that all values are > 0)

SELECT Grp, POWER(10.0, SUM(LOG10(Value))) AS Product
FROM YourTable
GROUP BY Grp;

(Advanced version - handles 0 and <0 values gracefully)

SELECT Grp,
CASE
WHEN MAX(CASE WHEN Value = 0 THEN 1 END) = 1 THEN 0
ELSE CASE
WHEN COUNT(CASE WHEN val < 0 THEN 1 END) % 2 = 0
THEN 1 ELSE -1
END * POWER(10.0, SUM(LOG10(NULLIF(ABS(Value),0))))
END AS Product
FROM YourTable
GROUP BY Grp;

(Advanced version, written in an even more incomprehensible way but
maybe a bit faster)

SELECT Grp,

CAST(ROUND(EXP(SUM(LOG(ABS(NULLIF(Value,0)))))*(1-SUM(1-SIGN(Value))%4)*(1-SUM(1-SQUARE(SIGN(val)))),0)
AS INT) AS Product
FROM YourTable
GROUP BY Grp;

These techniques are all described and explained in Itzik Ben-Gan's book
"Inside Microsoft SQL Server 2005 T-SQL Querying", page 358-360.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.