Home Forums Reporting Services Reporting Services Use a CTE to calculate running total instead of using the SSRS function? RE: Use a CTE to calculate running total instead of using the SSRS function?

  • pietlinden (8/18/2013)


    Getting closer, I think.... really should have heeded my professor's advice and used a smaller dataset, but anyway! I think this is right

    SELECT ProtocolNo

    , WeekNumber

    , Goal

    -- ADD RTGoal here

    , SUM(GOAL) OVER (PARTITION BY ProtocolNo

    ORDER BY WeekNumber

    ROWS UNBOUNDED PRECEDING) AS CumulativeGoal

    , NewEnrolls

    -- ADD RTEnrolls here

    , SUM(NewEnrolls) OVER (PARTITION BY ProtocolNo ORDER BY WeekNumber) AS CumulativeEnrolls

    , NewEnrolls - Goal AS OverUnder

    FROM

    (

    SELECT ProtocolNo

    , eg.WeekNumber

    , Goal

    , COUNT(e_PatientID) AS NewEnrolls

    FROM EnrollmentGoal eg

    LEFT JOIN Enroll e ON eg.ProtocolNo = e.e_ProtocolNo

    AND e.PWeek = eg.WeekNumber

    GROUP BY ProtocolNo, eg.WeekNumber, Goal

    ) x;

    Today's coffee is starting to wear off so I could be wrong but that doesn't look like a "running" total. It just looks like a weekly total. What version of Sql Server are you actually using?

    EDIT: It was the coffee wearing off... I total missed the ROWS UNBOUNDED PRECEDING directive.

    --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)