Thursday, March 1, 2012

Product() Aggregate Funcion

According to BOL, aggregate functions perform a calculation on a set of values and return a single value. However, I can't find a PRODUCT() function. Looks like it's not one of the aggregate functions provided by T-SQL.

Consider this data set:
custid      Factor
----------- -----------
1           2
1           3
2           4
2           5
2           2
3           6
3           7
3           3
4           8
4           9
To produce this result set:
custid      Product
----------- ----------
1           6
2           40
3           126
4           72
Here's the code:
select custid, POWER(10.,SUM(LOG10(Factor)))
from SampleTable
group by custid
As usual, comments are welcome...


 ~~ CK