Click here to Skip to main content
12,554,869 members (55,407 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

Aggregate Product function extends SQL

, 19 Feb 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
Technique to extend capability of standard SQL by adding the Aggregate Product Function


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

SQL Aggregate Product function based on this Math transfom

Fig.1. SQL Aggregate Product function based on this underlying Math transfom

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:

P(Xi)=X1*X2*...XN .................................................(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.


1. Aggregate Product function extends SQL


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


About the Author

President Infosoft International Inc
United States United States
Dr. A. Bell is a full-stack software developer (Win/Web/Mobile). He holds PhD in EE/IT, published 300+ articles, authored 37 inventions and is credited for 10+ Enterprise level projects; currently focused on HTML5/CSS3, Javascript/jQuery, .NET/WPF/C#, Android/Angular.js, 'Big Data', AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Sample projects/pubs:
  1. Real-time NY Bus monitoring app (IoT)
  2. Semaphon™ semantic phone num-to-text converter
  3. Educational Web Portal
  4. Free Online NY Payroll Tax Calculator
  5. WebTV powered by Embedded YouTube Player (Goog #1 YouTube API for ASP.NET)
  6. Top-50 Digital Cameras (powered by iMark-DCAM rating engine)
  7. Pure CSS3 Slide Show
  8. Inflation Calculator
  9. CSS3 Modal Pop-up Dialog
  10. Multilingual Geocoder with Interactive Map
  11. Online Semantic Analyzer (Concordance Calculator)
  12. NY City Job Market and Agency Ratings
  13. Advanced CSS3 Table Formatting

You may also be interested in...


Comments and Discussions

Questiongeneralization to the level of a semigroup? Pin
Vadim Stadnik20-Feb-15 0:08
memberVadim Stadnik20-Feb-15 0:08 
AnswerRe: generalization to the level of a semigroup? Pin
DrABELL21-Feb-15 10:36
professionalDrABELL21-Feb-15 10:36 
GeneralMy vote of 5 Pin
Scott Burkow20-Feb-13 7:31
memberScott Burkow20-Feb-13 7:31 
GeneralRe: My vote of 5 Pin
DrABELL20-Feb-13 18:46
memberDrABELL20-Feb-13 18:46 
GeneralRe: My vote of 5 Pin
Scott Burkow21-Feb-13 8:21
memberScott Burkow21-Feb-13 8:21 
GeneralRe: My vote of 5 Pin
DrABELL21-Feb-13 11:39
memberDrABELL21-Feb-13 11:39 
GeneralRe: My vote of 5 Pin
Scott Burkow22-Feb-13 5:56
memberScott Burkow22-Feb-13 5:56 
GeneralRe: My vote of 5 Pin
DrABELL22-Feb-13 7:44
memberDrABELL22-Feb-13 7:44 
GeneralRe: My vote of 5 Pin
Scott Burkow22-Feb-13 8:40
memberScott Burkow22-Feb-13 8:40 
GeneralReason for my vote of 5 Excellent example of using a little ... Pin
MacMaverick23-Mar-11 1:51
memberMacMaverick23-Mar-11 1:51 
GeneralRe: Many thanks! Kind regards/wishes, Alex Pin
DrABELL23-Mar-11 3:57
memberDrABELL23-Mar-11 3:57 
GeneralReason for my vote of 5 Simple & unusual Pin
A.J.Wegierski14-Mar-11 19:52
memberA.J.Wegierski14-Mar-11 19:52 
GeneralRe: Many thanks! Pin
DrABELL15-Mar-11 3:33
memberDrABELL15-Mar-11 3:33 
GeneralShort URL to the article: Pin
DrABELL4-Jan-11 9:37
memberDrABELL4-Jan-11 9:37 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 19 Feb 2015
Article Copyright 2010 by DrABELL
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid