I am trying to convert a model built in excel over to sql server 2005. My goal is to create a dynamic projection.
Specifically, the value should be set at the value specified in the database until the running total has reached half of the reserves and then decline at 20% from there.
TestModelInputs (16 entries)
yrID INT
FieldID INT = 1642
MYear INT values are 2005 though 2015
TestModelInputs2 (1 entry)
vID
FieldID INT =1642
Peak INT =150
Reserves INT =500
StartDT DateTime =7/1/2008
PeakDT DateTime =7/1/2010
VIEW TestModelProjections
CREATE VIEW TestModelProjections
AS
SELECT TestModelInputs.FieldID
, TestModelInputs.MYear
, CASE
WHEN TestModelInputs.MYear < YEAR(TestModelInputs2.Star
tDT) THEN 0
WHEN TestModelInputs.MYear = YEAR(TestModelInputs2.Star
tDT) THEN ((CAST(TestModelInputs2.Pe
ak AS FLOAT) * CAST((365 - datepart(dy,TestModelInput
s2.StartDT
))AS FLOAT)/365))
WHEN TestModelInputs.MYear <= YEAR(TestModelInputs2.Peak
DT) THEN TestModelInputs2.Peak
ELSE TestModelInputs2.Peak
END AS Projection
FROM TestModelInputs
INNER JOIN TestModelInputs2 ON TestModelInputs.FieldID = TestModelInputs2.FieldID
Query
SELECT T1.FieldID
, T1.MYear
, OilProjection
, (select sum(OilProjection * .365) from TestModelProjections where MYear <= T1.MYear) 'RunningTotal'
FROM TestModelProjections T1 INNER JOIN
TestModelInputs2 ON T1.FieldID = TestModelInputs2.FieldID
This is the current result set
FieldID MYear Projection (000s) RunningTotal
----------- ----------- ---------------------- ----------------------
1642 2005 0 0
1642 2006 0 0
1642 2007 0 0
1642 2008 96.65 35.28
1642 2009 210 111.93
1642 2010 210 188.58
1642 2011 210 265.23
1642 2012 210 341.88
1642 2013 210 418.53
1642 2014 210 495.18
1642 2015 210 571.83
What I Want is for the decline to start after the running total has reached 50% of the reserves (250 in this case) and then decline out at a rate of 20% (ie. Previous years number * (1 0.2) ):
FieldID MYear Projection (000s) RunningTotal (Million)
----------- ----------- ---------------------- ----------------------
1642 2005 0 0
1642 2006 0 0
1642 2007 0 0
1642 2008 96.655 35.28
1642 2009 210 111.93
1642 2010 210 188.58
1642 2011 210 265.23
1642 2012 168 326.55
1642 2013 134.4 375.61
1642 2014 107.52 414.85
1642 2015 86.016 446.25