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 |
|
Elgasen
Starting Member
2 Posts |
Posted - 2009-03-19 : 20:24:39
|
| Hello!My table look like this:Column1 Column2 1 8 10 15 21 32 35 48I would like to count diff between value in Column2 and the previous value in Column1. like this:Column1 Column2 Resultcolumn 1 8 0 10 15 2 21 32 6 35 48 3How can I create this resultcolumn? Br Elgasen! |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2009-03-20 : 00:45:39
|
| DECLARE @t TABLE(Column1 int,Column2 int)insert @tselect 1 ,8 union allselect 10, 15 union allselect 21, 32 union allselect 35, 48SELECT A.Column1,A.Column2,ISNULL(A.Column1 - B.Column2,0) AS ResultcolumnFROM(SELECT Column1,Column2,ROW_NUMBER() OVER(ORDER BY Column1) AS Rownum FROM @t)ALEFT JOIN(SELECT Column1,Column2,ROW_NUMBER() OVER(ORDER BY Column1) AS Rownum FROM @t)BON A.rownum = B.rownum + 1Note that I have sorted columns by Column1. |
 |
|
|
|
|
|