SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Running Values ( Cummulative)


Running Values ( Cummulative)

Author
Message
farooq.hbs
farooq.hbs
SSC-Addicted
SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)

Group: General Forum Members
Points: 437 Visits: 194
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

WHERE mt.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
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42122 Visits: 20009
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
Exploring Recursive CTEs by Example Dwain Camps
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1182 Visits: 653
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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218239 Visits: 41995
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Mitesh Oswal
Mitesh Oswal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1182 Visits: 653
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
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42122 Visits: 20009
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
Exploring Recursive CTEs by Example Dwain Camps
farooq.hbs
farooq.hbs
SSC-Addicted
SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)

Group: General Forum Members
Points: 437 Visits: 194
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

WHERE mt.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


farooq.hbs
farooq.hbs
SSC-Addicted
SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)SSC-Addicted (437 reputation)

Group: General Forum Members
Points: 437 Visits: 194

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
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42122 Visits: 20009
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
Exploring Recursive CTEs by Example Dwain Camps
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218239 Visits: 41995
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search