Click here to Skip to main content
Click here to Skip to main content

Aggregate Product function extends SQL

By , 14 Mar 2011
 

Preamble

The suggested solution extends the capability of Structured Query Language (SQL) by adding the Aggregate Product function. Entire 'pure SQL' solution is encapsulated into a single query, portable to any SQL backed databases, for example, Microsoft Access or SQL Server.
 

1. Underlying Math Transforms

Standard SQL contains several aggregate functions (Sum, Count, Min, Max, etc.) with noticeable absence of aggregate Product. As a reminder, Product function P of multiple arguments (X1, X2,...XN) is defined as:
 
N
P(Xi)=X1*X2*...XN .................................................(1)
i=1
 
Database engine cannot perform the aggregate product calculation directly, but it can calculate sums. Simple mathematical transforms provide a workaround enabling to compute the product P by using the standard built-in mathematical Log(), Exp() and SQL aggregated Sum() functions; the core technique is illustrated by mathematical formulas (2) and (3):
 
Log(X1*X2*... XN)= Log(X1)+Log(X2)+...Log(XN) ......................(2),
 
N           N
P(Xi)= Exp(SUM(Log(Xi))) ............................................(3)
i=1         i=1
 
The last formula (3) could be translated into SQL statement in a rather straightforward manner, enabling the calculation of aggregate Product by means of standard built-in SQL functions.
 

2. Programming Technique: Math-to-SQL Translation

 
This simple yet practical example will demonstrate the SQL programming technique enabling to calculate the Product of all positive numbers {2, 4, 5, 7, 8} stored in a Microsoft Access Table1. Based on the precondition that there are no any negative values, a simple SQL query can do the job of calculating Product (SQL 1):
 
SELECT Exp(Sum(Log([Num]))) AS P FROM Table1
 
The statement could be modified with IIf() conditional operator added in order to handle zeros(SQL 2):
 
SELECT Exp(Sum(IIf([Num]=0,0,Log([Num]))))*IIf(Min([Num])=0,0,1) AS P
FROM Table1
 
The solution has been implemented/tested in Microsoft Access 2003/2007; it is also portable to any other SQL-backed Database. For detailed discussion of this SQL technique, please refer to the online article [1], published by the author and included in the reference section.
 

References

1. Aggregate Product function extends SQL[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DrABELL
Chief Technology Officer Infosoft Int'l
United States United States
Member
Dr. A. Bell has 20+ years of SW/EE experience, published 200+ tech articles and authored 37 inventions; Win/Web veteran, currently focused on: HTML5, CSS3, Javascript, jQuery, SQL, Windows 8, .NET, C#, WPF, Ultrabooks, Mobile. Developed popular Silverlight Media Player, 3 Fractions Calculator and best YouTube API for ASP.NET (#1 Goog). Sample pubs/projects:
  1. HTML5 Best Practices: Table formatting via CSS3
  2. Personal computer 2012
  3. New iPad: notes from NY Apple store
  4. YouTube and Facebook popularity metrics
  5. Edumatter M12: School Math Calculators and Equation Solvers
  6. How to select web browser and check its capabilities
  7. SQL generates large data sequence
  8. Aggregate Product function extends SQL
  9. Top-50 Digital Cameras
  10. Evolution of digital cameras
  11. WebTV Project: Embedded YouTube Player

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberScott Burkow20 Feb '13 - 7:31 
I independently came up with the same solution--please see my article "The Product Aggregate in T-SQL Versus the CLR"--but I also adjusted for negative numbers. Is there a reason you left it off?
GeneralRe: My vote of 5memberDrABELL20 Feb '13 - 18:46 
Hi Scott,
Thanks for your message. I understand that you came to similar idea implemented in T-SQL. As FYI: the universal solution, which is applicable to both positive and negative numbers as well is described in my referenced publication (see the link).
Kind regards,
AB
GeneralRe: My vote of 5memberScott Burkow21 Feb '13 - 8:21 
yes there it is, Listing 4; great article
GeneralRe: My vote of 5memberDrABELL21 Feb '13 - 11:39 
Many Thanks! Btw, the solution is about 10 years old yet still practical. Feel free to reference that publication in your article. Best regards/wishes, AB
GeneralRe: My vote of 5memberScott Burkow22 Feb '13 - 5:56 
please verify the discussion in the new Afterword section when it is added online
GeneralRe: My vote of 5memberDrABELL22 Feb '13 - 7:44 
I guess it should read "Afterword" in your article, the same way you put it in the comments (just minor typo). Regards, AB
GeneralRe: My vote of 5memberScott Burkow22 Feb '13 - 8:40 
oh geez
GeneralReason for my vote of 5 Excellent example of using a little ...memberMacMaverick23 Mar '11 - 1:51 
Reason for my vote of 5
Excellent example of using a little thought, 'outside-the-square' maths and sql that parsimoniously solves the problem. Brilliant.
GeneralRe: Many thanks! Kind regards/wishes, AlexmemberDrABELL23 Mar '11 - 3:57 
Many thanks! Kind regards/wishes, Alex
GeneralReason for my vote of 5 Simple & unusualmemberA.J.Wegierski14 Mar '11 - 19:52 
Reason for my vote of 5
Simple & unusual

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 14 Mar 2011
Article Copyright 2010 by DrABELL
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid