49,777 Members
2 added today
332,753 Resources
164 added today

All Devdex   All Gurus  

Product aggregate function
Author: Joe Celko
Rating: Rate this Resource
Visits: 3106

Discuss in Newsgroups

Page:

The expression for the product of a column from logarithm and exponential functions is:

SELECT ((EXP (SUM (LN (CASE WHEN nbr = 0.00
                            THEN NULL
                            ELSE ABS(nbr) END))))
  * (CASE WHEN MIN (ABS (nbr)) = 0.00
          THEN 0.00
          ELSE 1.00 END)
  * (CASE WHEN MOD (SUM (CASE WHEN SIGN(nbr) = -1
                               THEN 1
                               ELSE 0 END), 2) = 1
           THEN -1.00
           ELSE 1.00 END) AS big_pi
 FROM NumberTable;

The nice part of this is that you can also use the SUM (DISTINCT <expression>) option to get the equivalent of PRD (DISTINCT <expression>).

You should watch the data type of the column involved and use either integer 0 and 1 or decimal 0.00 and 1.00 as is appropriate in the CASE statements.  It is worth studying the three CASE expressions that make up the terms of the Prod calculation.

The first CASE expression is to insure that all zeros and negative numbers are converted to a non-negative or NULL for the SUM() function, just in case your SQL raises an exception.

The second CASE expression will return zero as the answer if there was a zero in the nbr column of any selected row.  The MIN(ABS(nbr)) is a handy trick for detecting the existence of a zero in a list of both positive and negative numbers with an aggregate function.

The third CASE expression will return minus one if there was an odd number of negative numbers in the nbr column.  The innermost CASE expression uses a SIGN() function which returns +1 for a positive number, -1 for a negative number and 0 for a zero.  The SUM() counts the -1 results then the MOD() functions determines if the count was odd or even.


Next Page >>

Visitor Comments

Be the first to rate this article!

 

Rate this Article







	
	
	



ASP.NET Web Hosting
- FREE Setup & Domain
- First month FREE
100% IIS6 / Server 2003

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2010 Developersdex™. All rights reserved.