Solved

is there a product function in oracle

Posted on 2012-04-12
5
494 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to take different types of Oracle backups using RMAN.

829 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