Home » SQL & PL/SQL » SQL & PL/SQL » Multiplication function like SUM() ?
Multiplication function like SUM() ? [message #158286] Thu, 09 February 2006 13:57 Go to next message
rlodina
Messages: 3
Registered: February 2006
Location: Baia Mare
Junior Member

Hi,

Is there a function (agregate) for calculate multiplication of values form a field like Sum()

EX: Table_A

Name Note
------ ------
XX 2
YY 3

SELECT <Multiplication function>(Note) as Mult_Note FROM Table_A

Result:
Mult_Note
----------
6

(2*3 = 6)

Any ideea or workaround is welcome.
Thanks in advance.

Regards
Radu Lodina
Re: Multiplication function like SUM() ? [message #158293 is a reply to message #158286] Thu, 09 February 2006 14:49 Go to previous messageGo to next message
scottwmackey
Messages: 515
Registered: March 2005
Senior Member
Sorry, no suggestion here, but I am very curious to know that situation that requires this solution. That number could get very high very quickly when multiplying just a few rows.
Re: Multiplication function like SUM() ? [message #158297 is a reply to message #158293] Thu, 09 February 2006 15:03 Go to previous messageGo to next message
rlodina
Messages: 3
Registered: February 2006
Location: Baia Mare
Junior Member

Hi,

Thank you for reply.

Yes I agree.

I have this
0 ... ~1500 records with note between 0 and 100
1 ... ~200.000 records with note between 0 and 1
SELECT <Multiplication function>(Note) as Mult_Note 
   FROM  --here I have a bunch of sub query
 ( SELECT .... FROM ... ORDER BY Note )


Theoretically (from business perspective) the result will not exceed Numeric(20,2).

Regards
Radu Lodina
Re: Multiplication function like SUM() ? [message #158301 is a reply to message #158286] Thu, 09 February 2006 15:27 Go to previous messageGo to next message
mchadder
Messages: 224
Registered: May 2005
Location: UK
Senior Member
Hello,

I agree it's a strange request, but the first thing that pops into my head is a custom aggregate function :
SQL> CREATE OR REPLACE TYPE multiply_type AS OBJECT
  2  (
  3     total NUMBER,
  4  
  5     static function
  6          ODCIAggregateInitialize(sctx IN OUT multiply_type )
  7          return number,
  8  
  9     member function
 10          ODCIAggregateIterate(self IN OUT multiply_type ,
 11                               value IN number )
 12          return number,
 13  
 14     member function
 15          ODCIAggregateTerminate(self IN multiply_type,
 16                                 returnValue OUT  number,
 17                                 flags IN number)
 18          return number,
 19  
 20     member function
 21          ODCIAggregateMerge(self IN OUT multiply_type,
 22                             ctx2 IN multiply_type)
 23          return number
 24  );
 25  /

Type created.

SQL> CREATE OR REPLACE TYPE BODY multiply_type
  2  is
  3  
  4  static function ODCIAggregateInitialize(sctx IN OUT multiply_type)
  5  return number
  6  is
  7  begin
  8      sctx := multiply_type( null );
  9      return ODCIConst.Success;
 10  end;
 11  
 12  member function ODCIAggregateIterate(self IN OUT multiply_type,
 13                                       value IN number )
 14  return number
 15  is
 16  begin
 17      self.total := nvl(self.total, 1) * value;
 18      return ODCIConst.Success;
 19  end;
 20  
 21  member function ODCIAggregateTerminate(self IN multiply_type,
 22                                         returnValue OUT number,
 23                                         flags IN number)
 24  return number
 25  is
 26  begin
 27      returnValue := self.total;
 28      return ODCIConst.Success;
 29  end;
 30  
 31  member function ODCIAggregateMerge(self IN OUT multiply_type,
 32                                     ctx2 IN multiply_type)
 33  return number
 34  is
 35  begin
 36      self.total := ctx2.total;
 37      return ODCIConst.Success;
 38  end;
 39  
 40  END;
 41  /

Type body created.

SQL> 
SQL> CREATE OR REPLACE FUNCTION multiply_func(input number)
  2    RETURN number
  3  PARALLEL_ENABLE AGGREGATE USING multiply_type;
  4  /

Function created.

SQL> SELECT multiply_func(x.sum_value)
  2    FROM ( SELECT SUM(1) sum_value FROM dual
  3            UNION ALL
  4           SELECT SUM(3) FROM dual
  5            UNION ALL
  6           SELECT SUM(5) FROM dual ) x
  7  /

MULTIPLY_FUNC(X.SUM_VALUE)
--------------------------
                        15

Seems to work OK, but I haven't thoroughly tested it....

Rgds

Re: Multiplication function like SUM() ? [message #158309 is a reply to message #158286] Thu, 09 February 2006 18:01 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
You can combine EXP and LN, along with a judiciously placed SUM, to get the product of multiple factors.
SQL> CREATE TABLE t (id NUMBER, factor NUMBER)
  2  /
 
Table created.
 
SQL> INSERT INTO t VALUES (1,10);
SQL> INSERT INTO t VALUES (2,2);
SQL> INSERT INTO t VALUES (2,3);
SQL> INSERT INTO t VALUES (2,4);
SQL> INSERT INTO t VALUES (3,3.16);
SQL> INSERT INTO t VALUES (3,1.25);
SQL> INSERT INTO t VALUES (3,1.50);
SQL> INSERT INTO t VALUES (3,1.20);
 
SQL> SELECT   t.id
  2  ,        EXP(SUM(LN(t.factor))) product
  3  FROM     t
  4  GROUP BY t.id
  5  ORDER BY t.id
  6  /
 
        ID    PRODUCT
---------- ----------
         1         10
         2         24
         3       7.11
 
SQL>
Knowing your data, you'll have to figure out if your solution needs to be robust enough to handle zeroes or negatives.
Re: Multiplication function like SUM() ? [message #158311 is a reply to message #158286] Thu, 09 February 2006 18:09 Go to previous messageGo to next message
aketi
Messages: 26
Registered: October 2005
Junior Member
select round(exp(sum(ln(Val))))
from (select 3 as Val from dual
union select 4 from dual
union select 5 from dual
union select 6 from dual);



http://oraclesqlpuzzle.hp.infoseek.co.jp/2-3-16.html

Re: Multiplication function like SUM() ? [message #158337 is a reply to message #158286] Fri, 10 February 2006 01:00 Go to previous messageGo to next message
rlodina
Messages: 3
Registered: February 2006
Location: Baia Mare
Junior Member

Hi,

Thanks - to all.

Your solutions and sugestions was ok for me.

Best regards
Radu Lodina

Re: Multiplication function like SUM() ? [message #158528 is a reply to message #158337] Sun, 12 February 2006 23:53 Go to previous message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
A more thorough solution handling zeros and negatives was provided here by zozogirl.
Previous Topic: DML Trigger
Next Topic: MAX + DECODE + GROUP BY problem
Goto Forum:
  


Current Time: Tue Aug 19 02:44:18 CDT 2025