Solved

is there a product function in oracle

Posted on 2012-04-12
5
496 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fill Null values 5 56
How to find the cost of a stored procedure in Oracle and optimize it ?? 2 56
setting local variables in a cursor block 3 44
plsql job on oracle 18 109
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to recover a database from a user managed backup

752 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