Running Values ( Cummulative)

  • Hi All,

    Date Item_Name Value Cummulative_Value

    2012-01-01 Actual 5 5

    2012-02-01 Actual 5 10

    2012-03-01 Actual 10 20

    2012-04-01 Actual 5 25

    2012-05-01 Actual 5 30

    2012-06-01 Actual 5 35

    2012-07-01 Actual 5 40

    2012-08-01 Actual 5 45

    2012-09-01 Actual 5 50

    2012-10-01 Forcast 2 2

    2012-11-01 Forcast 1 3

    2012-01-01 Planned 1 1

    2012-02-01 Planned 1 2

    2012-03-01 Planned 2 4

    2012-04-01 Planned 3 7

    2012-05-01 Planned 4 11

    2012-06-01 Planned 5 16

    2012-07-01 Planned 6 22

    this above Grid From the table/ View object value is calculated value (Cummulative)from the value

    Now i want to do little more to this query

    I want to cummulate item_name = Actual and Forecast in the one coulmn..

    Planned Item will remain same.

    I used the below logic to cummulate the actual,Forcast and Planned Value

    Declare @date DATETIME

    Set @date = GETDATE()

    DECLARE @metric TABLE(mdate DATETIME, value FLOAT, item VARCHAR(100))

    INSERT INTO @metric

    SELECT m.mdate, m.value, mt.item_name--, mi.linked_project_id

    FROM View_Metric_Template mt

    INNER JOIN View_Metric_Instance mi ON mt.metric_template_id = mi.metric_template_id

    INNER JOIN fn_Metrics(null, null) m ON m.metric_instance_id = mi.metric_instance_id

    WHEREmt.item_id = m.template_item_id

    AND mi.linked_project_id = 'fs000080000jckclvqa0000000'

    AND mt.locale_id = N'en'

    AND mt.template_name = N'PS Progress Curves'

    AND mt.item_name IN ('C-Planned Progress %','C-Actual Progress %','C-Forecast Progress %')

    INSERT INTO @metric

    SELECT '2012-10-01',6,'Forcast'

    INSERT INTO @metric

    SELECT '2012-11-01',7,'Forcast'

    INSERT INTO @metric

    SELECT '2012-12-01',8,'Forcast'

    SELECT mdate,

    CASE WHEN item = 'C-Actual Progress %' AND mdate < @date THEN 'Actual'

    WHEN ITEM = 'Forcast' AND mdate > @date THEN 'Forecast'

    WHEN ITEM = 'C-Planned Progress %' THEN 'Planned'

    END AS ITEM_NAME,

    value,

    (SELECT SUM(value) FROM @metric WHERE item= t.item and mdate<=t.mdate )

    AS CumulativeSum

    FROM @metric t

    Order by item_name asc

    Now, how to cummulate the actual n forecast value..

    The Condition

    1.Actual Value should cummulate till the getdate()

    2.Forecast Value should cummulate after the Getdate() with Actual Value till Getdate()

    Example :

    Cummulate Actual Value till Getdate () = 22 then Forcast will Cummulate to begin From Greater than getdate() with 22 +..

    Hoping will get the Solution for this

    Thanks

  • farooq.hbs (9/4/2012)


    ...

    Hoping will get the Solution for this

    Thanks

    Of course - but you will get a solution much quicker if you can set up DDL and DML for some sample data. Click on the link in my sig, and read how to do this.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi ,

    Can you please try the below logic

    DECLARE @tbl TABLE (dt DATETIME,program VARCHAR(100),Value INT,RunningTotal INT)

    INSERT INTO @tbl(dt,program,Value)

    SELECT '1/1/2012','Actual', 5

    UNION ALL SELECT '1/2/2012','Actual', 5

    UNION ALL SELECT '1/3/2012','Actual', 1

    UNION ALL SELECT '1/4/2012','Actual', 5

    UNION ALL SELECT '1/5/2012','Actual', 5

    UNION ALL SELECT '1/6/2012','Actual', 5

    UNION ALL SELECT '1/7/2012','Actual', 5

    UNION ALL SELECT '1/8/2012','Actual', 5

    UNION ALL SELECT '1/9/2012','Actual', 5

    UNION ALL SELECT '1/10/2012','Forcast', 2

    UNION ALL SELECT '1/11/2012','Forcast', 1

    UNION ALL SELECT '1/12/2012','Planned', 1

    UNION ALL SELECT '1/13/2012','Planned', 1

    UNION ALL SELECT '1/14/2012','Planned', 2

    UNION ALL SELECT '1/15/2012','Planned', 3

    UNION ALL SELECT '1/16/2012','Planned', 4

    UNION ALL SELECT '1/17/2012','Planned', 5

    UNION ALL SELECT '1/18/2012','Planned',6

    ;WITH CTE AS

    (

    SELECT dt,program,Value,RunningTotal,ROW_NUMBER() OVER(PARTITION BY Program ORDER BY dt) AS RN

    FROM @tbl

    )

    select c.dt,c.program,c.value , sum(c2.value)RunningTotal from CTE c INNER JOIN cte c2

    on c.program = c2.program AND c.RN >=c2.RN

    group by c.dt,c.program,c.value

    order by c.program,c.dt

    Regards,
    Mitesh OSwal
    +918698619998

  • Mitesh Oswal (9/4/2012)


    Hi ,

    Can you please try the below logic

    I did and, as expected, it contains a triangular join. Please see the following article for why that will be absolutely devestating to the server for larger rowcounts.

    http://www.sqlservercentral.com/articles/T-SQL/61539/

    Either use a Quirky Update, a recursive CTE that doesn't use a Triangular Join, or use a well formed static, forward only, read only cursor. It'll be a lot faster and a whole lot easier on recources than almost anything (there is an exception but don't have time to find it right now) that uses a Triangular Join.

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

  • Farooq,

    Can you please verify the below code. Looks better performance.

    DECLARE @tbl TABLE (ID int identity(1,1),dt DATETIME,program VARCHAR(100),Value INT,RunningTotal INT)

    INSERT INTO @tbl(dt,program,Value)

    SELECT '1/1/2012','Actual', 5

    UNION ALL SELECT '1/2/2012','Actual', 5

    UNION ALL SELECT '1/3/2012','Actual', 10

    UNION ALL SELECT '1/4/2012','Actual', 5

    UNION ALL SELECT '1/5/2012','Actual', 5

    UNION ALL SELECT '1/6/2012','Actual', 5

    UNION ALL SELECT '1/7/2012','Actual', 5

    UNION ALL SELECT '1/8/2012','Actual', 5

    UNION ALL SELECT '1/9/2012','Actual', 5

    UNION ALL SELECT '1/10/2012','Forcast', 2

    UNION ALL SELECT '1/11/2012','Forcast', 1

    UNION ALL SELECT '1/12/2012','Planned', 1

    UNION ALL SELECT '1/13/2012','Planned', 1

    UNION ALL SELECT '1/14/2012','Planned', 2

    UNION ALL SELECT '1/15/2012','Planned', 3

    UNION ALL SELECT '1/16/2012','Planned', 4

    UNION ALL SELECT '1/17/2012','Planned', 5

    UNION ALL SELECT '1/18/2012','Planned',6

    DECLARE @RunningTotal INT

    ;WITH CTE AS

    (

    SELECT ID,dt,program,Value,RunningTotal,ROW_NUMBER() OVER(PARTITION BY Program ORDER BY dt) AS RN,

    DENSE_RANK() OVER(ORDER BY Program)RankD

    FROM @tbl

    )

    ,CTE2 AS

    (

    select dt,program,Value,Value AS RunningTotal,RN,RankD,

    MAX(RN) OVER(PARTITION BY RANKD) AS MAXRN

    FROM CTE

    WHERE RN = 1

    UNION ALL

    SELECT cte2.dt,cte2.program,cte.Value,cte2.RunningTotal+cte.value AS RunningTotal,cte2.RN,cte2.RankD,

    MAX(cte2.MAXRN+1) OVER(PARTITION BY cte2.RANKD) AS MAXRN

    FROM CTE INNER JOIN CTE2 ON CTE.RankD = CTE2.RankD AND CTE.RN = CTE2.MAXRN+1

    )

    select dt,Program,Value,RunningTotal from CTE2

    order by Program,dt

    Regards,
    Mitesh OSwal
    +918698619998

  • Mitesh Oswal (9/4/2012)


    Farooq,

    Can you please verify the below code. Looks better performance...

    Very interesting. Can you post the test script which you used for the comparison?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thanks for the reply..

    I Have written my own logic which is below...

    In this logic... I'm unable to do much for the missing month Between Actual and Forecast series..

    The Logic is Actual till the current and forecast From the Current month till the last month of the data present in the database. Actual n Forecast cummulate the values..

    Is there any solution for Missing Between Actual and Forcast

    Declare @date DATETIME

    Set @date = GETDATE()

    DECLARE @metric TABLE(mdate DATETIME, value FLOAT, item VARCHAR(100),item_name VARCHAR(150))

    INSERT INTO @metric

    SELECT m.mdate,m.value, mt.item_name,

    CASE WHEN mt.item_name = 'Planned Commitment' THEN 'Planned Commit'

    WHEN mt.item_name = 'Actual Commitment' AND mdate < @date THEN 'Actual Commit'

    WHEN mt.item_name = 'Forecast Commitment' AND mdate > @date THEN 'Actual Commit'

    WHEN mt.item_name = 'Planned Expenditure' THEN 'Planned Expen'

    WHEN mt.item_name = 'Actual Expenditure' AND mdate < @date THEN 'Actual Expen'

    WHEN mt.item_name = 'Forecast Expenditure' AND mdate > @date THEN 'Actual Expen'

    END AS ITEM_NAME

    FROM View_Metric_Template mt

    INNER JOIN View_Metric_Instance mi ON mt.metric_template_id = mi.metric_template_id

    INNER JOIN fn_Metrics(null, null) m ON m.metric_instance_id = mi.metric_instance_id

    WHEREmt.item_id = m.template_item_id

    AND mi.linked_project_id = 'fs000080000jbf28mna0000000'

    AND mt.locale_id = N'en'

    AND mt.template_name = 'PS - Commitment Expenditure'

    AND mt.item_name IN ('Planned Commitment','Actual Commitment','Forecast Commitment','Planned Expenditure','Actual Expenditure','Forecast Expenditure')

    SELECT mdate AS mdate1,

    REPLACE(RIGHT(CONVERT(VARCHAR(9), mdate, 6), 6), ' ', '-') AS mdate,

    CASE item WHEN 'Planned Commitment' THEN 'Plan Comit'

    WHEN 'Actual Commitment' THEN 'Actual Comit'

    WHEN 'Forecast Commitment' THEN 'Forecast Comit'

    WHEN 'Planned Expenditure' THEN 'Plan Expen'

    WHEN 'Actual Expenditure' THEN 'Actual Expen'

    WHEN 'Forecast Expenditure' THEN 'Forecast Expen'

    END AS item_name,

    value,

    (SELECT SUM(value) FROM @metric WHERE item_name= m1.item_name AND mdate <= m1.mdate) AS Cummulative_Value

    FROM @metric m1

    ORDER BY mdate1, item desc

  • This is sample table

    EventYear EventMonth Item_name Value Cummulative_Total

    2012 9 Forcast 1 1

    2012 1 Actual 2 3

    2012 10 Forcast 3 9

    2012 2 Actual 4 13

    2012 11 Forcast 5 18

    2012 3 Actual 6 24

    2012 12 Forcast 7 31

    2012 4 Actual 8 39

    But I need:

    EventYear EventMonth Item_name Value Cummulative_Total

    2012 9 Forcast 1 13

    2012 1 Actual 2 2

    2012 10 Forcast 3 16

    2012 2 Actual 4 6

    2012 11 Forcast 5 21

    2012 3 Actual 6 12

    2012 12 Forcast 7 28

    2012 4 Actual 8 12

    2012 5 Actual 0 12

    2012 6 Actual 0 12

    2012 7 Actual 0 12

    2012 8 Actual 0 12

    this is how i need..cummulate Value With Missing Month Actual till Getdate()

    Forcast from Getdate()

  • ChrisM@Work (9/4/2012)


    farooq.hbs (9/4/2012)


    ...

    Hoping will get the Solution for this

    Thanks

    Of course - but you will get a solution much quicker if you can set up DDL and DML for some sample data. Click on the link in my sig, and read how to do this.

    Your example query and "sample output" don't match, the column names and datatypes are different. Anyone trying to help you will be guessing. What we need is a sample data set in the form of a CREATE TABLE statement followed by INSERT statements, so that folks can copy it and run it in SSMS to generate a table to run sample queries against. Your problem is trivial; providing you with a solution is next to impossible without your cooperation.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • farooq.hbs (9/5/2012)


    This is sample table

    EventYear EventMonth Item_name Value Cummulative_Total

    2012 9 Forcast 1 1

    2012 1 Actual 2 3

    2012 10 Forcast 3 9

    2012 2 Actual 4 13

    2012 11 Forcast 5 18

    2012 3 Actual 6 24

    2012 12 Forcast 7 31

    2012 4 Actual 8 39

    But I need:

    EventYear EventMonth Item_name Value Cummulative_Total

    2012 9 Forcast 1 13

    2012 1 Actual 2 2

    2012 10 Forcast 3 16

    2012 2 Actual 4 6

    2012 11 Forcast 5 21

    2012 3 Actual 6 12

    2012 12 Forcast 7 28

    2012 4 Actual 8 12

    2012 5 Actual 0 12

    2012 6 Actual 0 12

    2012 7 Actual 0 12

    2012 8 Actual 0 12

    this is how i need..cummulate Value With Missing Month Actual till Getdate()

    Forcast from Getdate()

    Please post the data in a readily consumable format. See the first link in my signature line below for how to easily do that. Thanks for helping us help you.

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

Viewing 10 posts - 1 through 9 (of 9 total)

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