| Author |
Topic |
|
rocco2008
Yak Posting Veteran
63 Posts |
Posted - 2008-09-30 : 14:18:56
|
| Hi,I have a table where I have a price column. I am trying to calculate the percentage change of the price from row to row. Anybody any idea how to do this?Many thanks in advance!! |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-09-30 : 15:10:31
|
One way is do a self join. One query using 2 instances of the same table in the FROM clause. You need something in the table to define the sequence of rows. Here is an example:declare @t table (tranDate datetime, price money)insert @tselect '2008-1-1 10:20:02.000', 10 union allselect '2008-1-1 10:20:03.000', 11 union allselect '2008-1-2 00:00:00.000', 9 union allselect '2008-1-5 00:00:00.000', 9.23 ;with seq (tranDate, price, seq)as(select tranDate ,price ,row_number() over (order by tranDate)from @t)select curr.tranDate ,curr.price ,(curr.price-prev.price) / prev.price as pctChangefrom seq currleft outer join seq prev on prev.seq+1 = curr.seqoutput:tranDate price pctChange----------------------- --------------------- ---------------------2008-01-01 10:20:02.000 10.00 NULL2008-01-01 10:20:03.000 11.00 0.102008-01-02 00:00:00.000 9.00 -0.18182008-01-05 00:00:00.000 9.23 0.0255 Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 00:33:06
|
ANd if you're using sql 2000:-SELECT IDENTITY(int,1,1) AS Seq,trandate,priceINTO #TempFROm YourTableORDER BY trandateSELECT t1.trandate,t1.price,(t1.price-COALESCE(t2.price,0)*100.0)/NULLIF(t2.price,0) AS increasepercentFROM #Temp t1LEFT JOIN #Temp t2ON t2.Seq=t1.Seq-1 |
 |
|
|
rocco2008
Yak Posting Veteran
63 Posts |
Posted - 2008-10-01 : 07:03:52
|
| Hi and many thanks for the answers. I was using the latter solution, but I get wrong values over and over, egTime Price IncreasePercent1 43.75 -99.02 43.880000000000003 -98.9970285714285723 43.93 -98.9988605287146584 44.0 -98.9984065558843585 44.0 -99.0I was trying to change the calculation (it's got to be something like [(b.price/a.price) -1]), since I think the bug is somewhere there, but nothing gets me the right solution.anybody an idea??many tnx again! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 07:12:31
|
quote: Originally posted by rocco2008 Hi and many thanks for the answers. I was using the latter solution, but I get wrong values over and over, egTime Price IncreasePercent1 43.75 -99.02 43.880000000000003 -98.9970285714285723 43.93 -98.9988605287146584 44.0 -98.9984065558843585 44.0 -99.0I was trying to change the calculation (it's got to be something like [(b.price/a.price) -1]), since I think the bug is somewhere there, but nothing gets me the right solution.anybody an idea??many tnx again!
what i've used is (currprice-prevprice)* 100.0/prevpricecan you post some sample data which you used (to get the ouput you posted). i'll try for that. |
 |
|
|
rocco2008
Yak Posting Veteran
63 Posts |
Posted - 2008-10-01 : 07:24:06
|
| That should be right..Sure, here's the data (at time 0, the increasepercentage value was NULL):0_____43.751_____43.752_____43.883_____43.934_____44.005_____44.006_____44.037_____43.938_____43.93thanks again |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 07:35:36
|
it was a matter of missing bracesSELECT IDENTITY(int,1,1) AS Seq,trandate,priceINTO #TempFROm YourTableORDER BY trandateSELECT t1.trandate,t1.price,((t1.price-COALESCE(t2.price,0))*100.0)/NULLIF(t2.price,0) AS increasepercentFROM #Temp t1LEFT JOIN #Temp t2ON t2.Seq=t1.Seq-1 |
 |
|
|
rocco2008
Yak Posting Veteran
63 Posts |
Posted - 2008-10-01 : 07:40:18
|
| you rule!tnx so much! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 07:42:28
|
quote: Originally posted by rocco2008 you rule!tnx so much!
welcome |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-10-01 : 08:21:49
|
| Be careful using SELECT IDENTITY(int,1,1)...INTO ...ORDER BYThe identity sequence is not garuanteed to be represented by the ORDER BY. In this case it is better to create the #temp table first then do a INSERT INTO #temp...SELECT...ORDER BYBe One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 08:32:45
|
quote: Originally posted by TG Be careful using SELECT IDENTITY(int,1,1)...INTO ...ORDER BYThe identity sequence is not garuanteed to be represented by the ORDER BY. In this case it is better to create the #temp table first then do a INSERT INTO #temp...SELECT...ORDER BYBe One with the OptimizerTG
but its working fine for medeclare @t table (tranDate datetime, price money)insert @tselect '2008-1-1 10:20:02.000', 10 union allselect '2008-1-1 10:20:03.000', 11 union allselect '2008-1-2 00:00:00.000', 9 union allselect '2008-1-5 00:00:00.000', 9.23 union allselect '2008-3-1 10:20:03.000', 21 union allselect '2008-02-20 00:00:00.000', 19.01 union allselect '2006-1-5 00:00:00.000', 19.23 union allselect '2006-11-10 10:20:03.000', 41 union allselect '2008-10-22 00:00:00.000', 90 union allselect '2008-1-15 00:00:00.000', 91.23 union allselect '2008-8-12 10:20:03.000', 110 union allselect '2008-6-2 00:00:00.000', 91 union allselect '2008-6-5 00:00:00.000', 23SELECT IDENTITY(int,1,1) AS Seq,trandate,priceINTO #TempFROM @tORDER BY trandateSELECT * FROM #temp ORDER BY Seq drop table #Tempoutput----------------------------------------Seq trandate price1 2006-01-05 00:00:00.000 19.232 2006-11-10 10:20:03.000 41.003 2008-01-01 10:20:02.000 10.004 2008-01-01 10:20:03.000 11.005 2008-01-02 00:00:00.000 9.006 2008-01-05 00:00:00.000 9.237 2008-01-15 00:00:00.000 91.238 2008-02-20 00:00:00.000 19.019 2008-03-01 10:20:03.000 21.0010 2008-06-02 00:00:00.000 91.0011 2008-06-05 00:00:00.000 23.0012 2008-08-12 10:20:03.000 110.0013 2008-10-22 00:00:00.000 90.00 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-10-01 : 08:52:53
|
| >>but its working fine for meI didn't say it never works, I said the sequence of the identity isn't GARUANTEED to match the order by.This topic has an example:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=93431Be One with the OptimizerTG |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
|