From this data (assuming the number of rows is not known in advance):
with q1 as (select mod(ora_hash(level),5) c1 from dual connect by level <=4)
select * from q1;
/*
C1
--
2
1
4
1
*/
I want the product of the c1 column from all the rows. Something like the results of SUM(c1) only I want each value multiplied by they others rather than added. In this case that would be 2 * 1 * 4 * 1 = 8.
/*
X1
--
8
*/
Data could contain negative numbers and zero, which can be simulated using:
with q1 as (select mod(ora_hash(level),5)-1 c1 from dual connect by level <=4)
select * from q1;
or
with q1 as (select mod(ora_hash(level),5)-3 c1 from dual connect by level <=4)
select * from q1;
I know this could be done with custom aggregate function, but am interested in native approaches.
Best Answer
For sufficiently small aggregate products, you can use the old trick of summing the logarithms and them exponentiating the result
Since you're using 11.2, it's a bit more verbose (though someone may be able to figure out a simpler version) but you can also use recursive common table expressions