running sum cursor

  • Unfortunately and based on the name of this particular forum, it looks like the OP is still using SQL Server 2005.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • This thread is SEVEN YEARS OLD, so it's not safe to make any assumptions about what the OP is currently using.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • RocksteadyDev wrote:

    T-SQL 2012 has a new built in function to give a running total, so you don't need a cursor for running totals that sum distinct counts. Previously sum and count functions could not be nested. Here is an example.

    SELECT date, sum(count(DISTINCT unique_id)) OVER (ORDER BY date) AS total_per_date

    FROM dbo.table

    GROUP BY date

    You still cannot nest aggregate functions. It doesn't make sense to do so. Your example does not work in any version of sql server to date.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange wrote:

    RocksteadyDev wrote:

    T-SQL 2012 has a new built in function to give a running total, so you don't need a cursor for running totals that sum distinct counts. Previously sum and count functions could not be nested. Here is an example.

    SELECT date, sum(count(DISTINCT unique_id)) OVER (ORDER BY date) AS total_per_date

    FROM dbo.table

    GROUP BY date

    You still cannot nest aggregate functions. It doesn't make sense to do so. Your example does not work in any version of sql server to date.

    You can nest an aggregate in a windowed function, which is exactly what is being done here.  And it can make sense to do so.  For instance, box office sales often report a daily total and a running total.

    SELECT sales_dt
    , SUM(sales_amount) AS daily_sales
    , SUM(SUM(sales_amount)) OVER(ORDER BY sales_dt ROWS UNBOUNDED PRECEDING) AS total_sales
    FROM
    (
    VALUES
    (1, '2019-09-14', 23567)
    ,(1, '2019-09-15', 83673)
    ,(1, '2019-09-16', 36173)
    ,(2, '2019-09-14', 31564)
    ,(2, '2019-09-15', 36243)
    ,(2, '2019-09-16', 61433)
    ,(3, '2019-09-14', 35644)
    ,(3, '2019-09-15', 53643)
    ,(3, '2019-09-16', 66143)
    ,(4, '2019-09-14', 35764)
    ,(4, '2019-09-15', 36483)
    ,(4, '2019-09-16', 61439)
    ) box_office(theater_id, sales_dt, sales_amount)
    GROUP BY sales_dt;

    Drew

    • This reply was modified 5 years, 3 months ago by  drew.allen. Reason: edited to break up long line of code

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen wrote:

    Sean Lange wrote:

    RocksteadyDev wrote:

    T-SQL 2012 has a new built in function to give a running total, so you don't need a cursor for running totals that sum distinct counts. Previously sum and count functions could not be nested. Here is an example.

    SELECT date, sum(count(DISTINCT unique_id)) OVER (ORDER BY date) AS total_per_date

    FROM dbo.table

    GROUP BY date

    You still cannot nest aggregate functions. It doesn't make sense to do so. Your example does not work in any version of sql server to date.

    You can nest an aggregate in a windowed function, which is exactly what is being done here.  And it can make sense to do so.  For instance, box office sales often report a daily total and a running total.

    SELECT sales_dt
    , SUM(sales_amount) AS daily_sales
    , SUM(SUM(sales_amount)) OVER(ORDER BY sales_dt ROWS UNBOUNDED PRECEDING) AS total_sales
    FROM
    (
    VALUES
    (1, '2019-09-14', 23567)
    ,(1, '2019-09-15', 83673)
    ,(1, '2019-09-16', 36173)
    ,(2, '2019-09-14', 31564)
    ,(2, '2019-09-15', 36243)
    ,(2, '2019-09-16', 61433)
    ,(3, '2019-09-14', 35644)
    ,(3, '2019-09-15', 53643)
    ,(3, '2019-09-16', 66143)
    ,(4, '2019-09-14', 35764)
    ,(4, '2019-09-15', 36483)
    ,(4, '2019-09-16', 61439)
    ) box_office(theater_id, sales_dt, sales_amount)
    GROUP BY sales_dt;

    Drew

    I guess I was hung up on the wording more than the code and failed to look at the code enough to realize it was using a window function. Derp!!!

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 5 posts - 31 through 34 (of 34 total)

You must be logged in to reply to this topic. Login to reply