cumulative calculation with partition by

Posted on 2009-02-17
Last Modified: 2013-12-07

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
Question by:seenall
    LVL 57

    Expert Comment

    by:Raja Jegan R
    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

    LVL 73

    Accepted Solution

    try this...

    you should only need to query the table once.
    The query above should work also but it looks like it was written for sql server where there isn't an analytic version of SUM which is more efficient.

    of course, don't trust, instead test,
    time all suggestions and use autotrace to measure the activity of each to determine which does less work on your system.
    SELECT friend, eventdate, timespent
      FROM (SELECT friend,
                   ROW_NUMBER() OVER (PARTITION BY friend ORDER BY eventdate) rn
              FROM (SELECT friend,
                               OVER (PARTITION BY friend ORDER BY eventdate ROWS UNBOUNDED PRECEDING)
                      FROM yourtable)
             WHERE sumtime >= 20)
     WHERE rn = 1

    Open in new window

    LVL 3

    Author Closing Comment

    Thanks very much for this - I think I understand the partition by statement now - until the next time :D - Great work
    LVL 73

    Expert Comment

    glad I could help.  analytics are definitely an awesome set of tools to learn.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

    759 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

    14 Experts available now in Live!

    Get 1:1 Help Now