Solved

is there a product function in oracle

Posted on 2012-04-12
5
495 Views
Last Modified: 2012-04-16
Want to be able to use product functionality as in excel.
Example..
Select * from mytable t1
Say this returns...
Cpy  Year       Amount
A      2008        10
A      2008         20
A      2009        40
B      2008         1
B      2008         2
B      2008         3
etc etc
So, I want to return a data set grouped by Cpy then Year with PRODUCT values
Cpy   Year    Amount
A      2008   200
A      2009     40
B      2008       6

Is this possible without using PLSQL
Regards
0
Comment
Question by:philsivyer
5 Comments
 
LVL 8

Accepted Solution

by:
Christoffer Swanström earned 200 total points
ID: 37836983
If the amounts are always > 0 you can do

SELECT
  cpy
  ,year
  ,exp(sum(ln(amount)))
FROM
  mytable
GROUP BY
  cpy
  ,year
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 100 total points
ID: 37837133
You can also create your own aggregate function, which doesn't require positive inputs.

In fact, a product aggregate is one of the examples in the article below


http://www.experts-exchange.com/Database/Oracle/A_9391-How-to-Create-User-Defined-Aggregates-in-Oracle.html


The aggregate itself is pl/sql, but is usable within sql like other aggregates

Using the builtin sql functions should be more efficient though if your inputs are legal
0
 
LVL 27

Assisted Solution

by:sujith80
sujith80 earned 200 total points
ID: 37845834
There is not built in product function within oracle. However you can do it with a recursive subquery factoring technique.

See below -

SQL> select * from tbl1;

CPY                        YEAR     AMOUNT
-------------------- ---------- ----------
A                          2008         10
A                          2008         20
A                          2009         40
B                          2008          1
B                          2008          2
B                          2008          3

6 rows selected.

SQL> with data as (
  2  select cpy, year, amount,
  3  row_number() over (partition by cpy, year order by(1)) rn,
  4  count(1) over (partition by cpy, year order by(1)) cn
  5  from tbl1
  6  ),
  7  x(cpy, year, amount, rn, cn) as (
  8  select cpy, year, amount, rn, cn
  9  from data
 10  where rn = 1
 11  union all
 12  select data.cpy, data.year, data.amount * x.amount, data.rn, data.cn
 13  from data, x
 14  where data.cpy = x.cpy
 15  and data.year=x.year
 16  and data.rn = x.rn+1)
 17  select cpy, year, amount
 18  from x
 19  where rn = cn
 20  order by cpy, year;

CPY                        YEAR     AMOUNT
-------------------- ---------- ----------
A                          2008        200
A                          2009         40
B                          2008          6

SQL>

Open in new window

0
 

Author Comment

by:philsivyer
ID: 37850500
Thanks everbody - great help
0
 
LVL 27

Expert Comment

by:sujith80
ID: 37851620
what is the solution that you used?
Did it completely resolve your problem?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select and Insert Query running slow 4 57
Oracle cursor lifecycle inside procedure. 2 24
subtr returning incorrect value 8 63
Oracle Errors 11 43
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question