Link to home
Start Free TrialLog in
Avatar of seenall
seenall

asked on

cumulative calculation with partition by


Typical record set as follows

Friend1    01/01/2009    5
Friend1    03/01/2009    4
Friend1    07/01/2009    6
Friend1    09/01/2009    3
Friend1    12/01/2009    6
Friend1    13/01/2009    4

Now - I want to find the first date when the accumulated timespent is equal or greater than 20. - so in the example above on 12 January the accumulated time was 24

The query should then halt at 12 and move onto the next friend.

Might this be done with an Analytical function? - I am using Oracle RDBMS with Data Warehousing
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Try this query:

This should help you out..
WITH cte ( col1, date1, count1, cnt) as
(SELECT col1, date1, count1, row_number() OVER ( ORDER BY date1 ASC) cnt
FROM urtable ) 
select col1, date1, count1, cumulative from 
(
SELECT t1.col1, t1.date1, t1.count1, (SELECT sum(cte.count1) FROM cte WHERE cte.cnt <= t1.cnt) cumulative
FROM cte t1) as tmp
where cumulative <= 20

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seenall
seenall

ASKER

Thanks very much for this - I think I understand the partition by statement now - until the next time :D - Great work
glad I could help.  analytics are definitely an awesome set of tools to learn.