Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
KingCarlos
Yak Posting Veteran
74 Posts |
Posted - 2013-10-24 : 20:18:53
|
SQL Calculation Help RequiredI have the following data / table of transactions I want to record in a temporary table. The starting value and the end value are calculation values except for the starting value for line 1.........Starting value....Movement....End Value Trans 1........100............10...........90Trans 2.........90............25...........65Trans 3.........65............30...........35 Notes The starting value 100 I will read from a starting positionThe movement column is data that will be extracted from elsewhereThe end value is a simple calculation from starting value – movementThe question I have is how to get the end value from line 1 to be the starting value in line 2 and then the end value in line 2 to be the starting value in line 3 and so forth? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-25 : 03:04:09
|
using CTEillustrationdeclare @t table(TransName varchar(30),Movement int)insert @tvalues ('Trans 1',10),('Trans 2',25),('Trans 3',30)DECLARE @StartValue intSET @StartValue = 100;With CTE1AS(SELECT ROW_NUMBER() OVER (ORDER BY TransName) AS Seq,*FROM @t),CTE2 AS(SELECT TransName,@StartValue AS StartValue,Movement,@StartValue - Movement AS EndValue,SeqFROM CTE1WHERE Seq=1UNION ALLSELECT c1.TransName,c2.EndValue,c1.Movement,c2.EndValue - c1.Movement,c1.SeqFROM CTE2 c2INNER JOIN CTE1 c1ON c1.Seq = c2.Seq + 1)SELECT TransName,EndValue,Movement,EndValueFROM CTE2OPTION (MAXRECURSION 0)ouput---------------------------------------------TransName StartValue Movement EndValue---------------------------------------------------------Trans 1 100 10 90Trans 2 90 25 65Trans 3 65 30 35 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-25 : 03:17:07
|
Using Quirly update methoddeclare @t table(TransName varchar(30),Movement int)insert @tvalues ('Trans 1',10),('Trans 2',25),('Trans 3',30),('Trans 4',15)create table #t(TransName varchar(30),StartValue int,Movement int,EndValue int)create clustered index idx_clust on #t(TransName asc)insert #t (TransName,Movement)SELECT TransName,MovementFROm @tDECLARE @StartValue int,@TransName varchar(30)SET @StartValue = 100UPDATE #t WITH (TABLOCK)SET @StartValue = StartValue =@StartValue,@StartValue = EndValue = @StartValue - Movement,@TransName = TransNameOPTION (MAXDOP 1)SELECT *FROM #t DROP TABLE #Toutput----------------------------------------------------------TransName StartValue Movement EndValue----------------------------------------------------------Trans 1 100 10 90Trans 2 90 25 65Trans 3 65 30 35Trans 4 35 15 20 Referhttp://visakhm.blogspot.in/2010/03/using-quirky-updates-to-develop-well.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2013-10-30 : 03:59:10
|
[code]declare @t table(TransName varchar(30),Movement int)insert @tvalues ('Trans 1',10),('Trans 2',25),('Trans 3',30)DECLARE @StartValue intSET @StartValue = 100select Transname, StartValue = lag(EndValue, 1, @StartValue) over (order by transname), Movement, EndValuefrom ( select *, EndValue = @StartValue - sum(movement) over (order by transname) from @t )src[/code] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-30 : 04:12:45
|
quote: Originally posted by waterduck
declare @t table(TransName varchar(30),Movement int)insert @tvalues ('Trans 1',10),('Trans 2',25),('Trans 3',30)DECLARE @StartValue intSET @StartValue = 100select Transname, StartValue = lag(EndValue, 1, @StartValue) over (order by transname), Movement, EndValuefrom ( select *, EndValue = @StartValue - sum(movement) over (order by transname) from @t )src
Good suggestionjust a caveat that works only in SQL 2012 and above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|