Solved

is there a product function in oracle

Posted on 2012-04-12
5
491 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
Comment Utility
If the amounts are always > 0 you can do

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

Assisted Solution

by:sdstuber
sdstuber earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks everbody - great help
0
 
LVL 27

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now