Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sum Over Time

908018Jan 4 2012 — edited Jan 5 2012
Hi,

I'm trying to do something which I would guess is quite a common query, but after scratching my head and perusing the web I am still no closer to a solution.

I am running:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

I'm looking to sum up a set of values, taking into account both a parent grouping and start and end dates.
For the parent grouping I am using:

+SUM([value]) over (Partition by [Parent] order by [Parent],[Child])+

And I was hoping to be able to extend this SUM to also handle the start and end dates, so the final output would contain a sum of the values for each different time period.

As an example, using the data below I'm trying to sum up the price of the components of a car over time:

row, product, component, rate, start date, end date
1, car, chassis, 180, 01/01/2000, 31/12/2009
2, car, chassis, 200, 01/01/2010, 01/01/2050
3, car, engine, 100, 01/01/2000, 01/01/2050


Notice there is a change of price for Component 'chassis', so the output I'm looking for is:


row, product, component, rate, start date, end date, sum
1, car, chassis, 180, 01/01/2000, 31/12/2009, 280
2, car, engine, 100, 01/01/2000, 31/12/2009, 280
3, car, chassis, 200, 01/01/2010, 01/01/2050, 300
4, car, engine, 100, 01/01/2010, 01/01/2050, 300


But in reality all I need is:

row, product, start date, end date, sum
1, car, 01/01/2000, 31/12/2009, 280
2, car, 01/01/2010, 01/01/2050, 300

Preferably the query would be in a view rather than a stored procedure, and it needs to be able to handle many 'products', 'components' and start/end dates.


All help most appreciated, and if any more info is required, please let me know.

Thanks,
Julian
This post has been answered by Frank Kulash on Jan 4 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2012
Added on Jan 4 2012
7 comments
571 views