Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Calculation


T-SQL Calculation

Author
Message
PSB
PSB
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 1452
Hi,


I have a table where I need to calculate Trend based on Week Type and Unit .


CREATE TABLE #REport
(
TYPE VARCHAR(20),
Unit VARCHAR(20),
Forecast INT,
Week INT,
Trend NUMERIC(9,2)
)

INSERT INTO #REport( TYPE, Unit, Forecast, Week, Trend )
SELECT 'Net Revenue','XXX',0,1,0
UNION
SELECT 'Net Revenue','XXX',0,2,0
UNION
SELECT 'Net Revenue','XXX',0,3,0
UNION
SELECT 'Net Revenue','XXX',0,4,0
UNION
SELECT 'Net Revenue','XXX',105000,5,0
UNION
SELECT 'Invest','XXX',0,1,0
UNION
SELECT 'Invest','XXX',0,2,0
UNION
SELECT 'Invest','XXX',0,3,0
UNION
SELECT 'Invest','XXX',0,4,0
UNION
SELECT 'Invest','XXX', 45951 ,5,0

SELECT * FROM #REport
DROP TABLE #REport

--FOR every TYPE AND Unit, the trend should be calculated AS follows :

--FOR Week 1 OF TYPE = 'Invest' and Unit = 'XXX'
--Trend = 45951/MAX week number = 45951/5 = 9190.2

--FOR Week 2 OF TYPE = 'Invest' and Unit = 'XXX'
--Trend = 9190.2 + 45951/5 = 18380.4-- Trend of Week 1 + 45951/5


--FOR Week 3 OF TYPE = 'Invest' and Unit = 'XXX'
--Trend = 18380.4 + 45951/5 = 27570.6-- Trend of Week 2 + 45951/5

--FOR Week 4 OF TYPE = 'Invest' and Unit = 'XXX'
--Trend = 27570.6 + 45951/5 = 36760.8-- Trend of Week 3 + 45951/5


--FOR Week 5 OF TYPE = 'Invest' and Unit = 'XXX'
--Trend = 36760.8 + 45951/5 = 45951-- Trend of Week 4 + 45951/5


Thanks,
PSB
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3456 Visits: 33049
just to be sure.....can you please provide the actual results you require based on your sample data......and not the calcs.

ta.

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

PSB
PSB
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 1452
Desired result ..

SELECT 'Invest' AS TYPE,'XXX' AS Unit,0 AS ForeCast,1 AS Week,9190.2 AS Trend
UNION
SELECT 'Invest' AS Type,'XXX' AS Unit,0 AS ForeCast,2 AS Week,18380.4 AS Trend
UNION
SELECT 'Invest' AS TYPE,'XXX' AS Unit,0 AS ForeCast,3 AS Week,27570.6 AS Trend
UNION
SELECT 'Invest' AS Type,'XXX' AS Unit,0 AS ForeCast,4 AS Week,36760.8 AS Trend
UNION
SELECT 'Invest' AS Type,'XXX' AS Unit, 45951 AS ForeCast,5 AS Week,45951 AS Trend
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
Otherwise you will get answers like this:

SELECT *, Trend = ([Week]/5.0) * MAX(Forecast) OVER(PARTITION BY [TYPE])
FROM #REport



Which exactly meets your requirements (apart from the trivial matter of rounding) but is almost certainly incorrect.

“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
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3456 Visits: 33049
ChrisM@Work (5/8/2014)
Otherwise you will get answers like this:

SELECT *, Trend = ([Week]/5.0) * MAX(Forecast) OVER(PARTITION BY [TYPE])
FROM #REport



Which exactly meets your requirements (apart from the trivial matter of rounding) but is almost certainly incorrect.


Chris ...your mind reading skills are quite extraordinary :-)
but have a suspicion that OP has oversimplified........we can wait and see

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
J Livingston SQL (5/8/2014)
ChrisM@Work (5/8/2014)
Otherwise you will get answers like this:

SELECT *, Trend = ([Week]/5.0) * MAX(Forecast) OVER(PARTITION BY [TYPE])
FROM #REport



Which exactly meets your requirements (apart from the trivial matter of rounding) but is almost certainly incorrect.


Chris ...your mind reading skills are quite extraordinary :-)
but have a suspicion that OP has oversimplified........we can wait and see


J Livingstone - you give me way too much credit for a Wild Guess :-D

“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
PSB
PSB
SSC-Addicted
SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)SSC-Addicted (489 reputation)

Group: General Forum Members
Points: 489 Visits: 1452
Thanks for the solution! it works perfectly .
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