Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a PRODUCT function like there is a SUM function in Oracle SQL?

Tags:

sql

oracle

I have a coworker looking for this, and I don't recall ever running into anything like that.

Is there a reasonable technique that would let you simulate it?

SELECT PRODUCT(X)
FROM
(
    SELECT 3 X FROM DUAL
    UNION ALL 
    SELECT 5 X FROM DUAL
    UNION ALL
    SELECT 2 X FROM DUAL
)

would yield 30

like image 452
EvilTeach Avatar asked Dec 31 '08 19:12

EvilTeach


2 Answers

select exp(sum(ln(col)))
  from table;

edit:

if col always > 0

like image 85
tuinstoel Avatar answered Sep 20 '22 19:09

tuinstoel


DECLARE @a int
SET @a = 1
-- re-assign @a for each row in the result
-- as what @a was before * the value in the row
SELECT @a = @a * amount
FROM theTable

There's a way to do string concat that is similiar:

DECLARE @b varchar(max)
SET @b = ""

SELECT @b = @b + CustomerName
FROM Customers
like image 23
Amy B Avatar answered Sep 19 '22 19:09

Amy B