| Author |
Topic |
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-05-30 : 12:05:35
|
| Hi all,I am new to SQL 2008 and am having trouble figuring out how to find the difference between two rows in the same column. Data: Real LossAmount fx 15 6087 0.05 12 2605 0.0519 2308 0.0511 2278 0.056 1770 0.0518 1060 0.0510 714 0.054 -3039 0.0514 -3954 0.058 -6955 0.059 -7522 0.052 -8637 0.0516 -9065 0.053 -11582 0.057 -13349 0.051 -14379 0.0520 -17526 0.055 -17730 0.0517 -21084 0.0513 -23634 0.05I want the following column: dy 3,483 (6087-2605) 297 (2605-2308) and so on 30 508 710 346 3,754 914 3,001 568 1,115 427 2,518 1,767 1,030 3,148 203 3,355 2,549 - The data is ordered by LossAmount in descending order and the name of the table is dbo.Data2 If anyone could help me with this topic, I would REALLY appreciate it! Thank you! |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-05-30 : 12:13:38
|
| with d as(select LossAmount, seq = row_number() over (order by LossAmount desc)from Data2)select d.LossAmount - d2.LossAmountfrom d djoin d d2on dseq = d2.seq + 1==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-05-30 : 12:25:57
|
| Thank you so much for your reply but SQL is not recognizing dseq as a column. Was that a typo? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-05-30 : 12:28:57
|
| yes - shoould be d.seqalso shoould bed2.seq = d.seq+1==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-05-30 : 12:31:34
|
| awesome. thank you so much for your help I really appreciate it! |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-30 : 13:54:14
|
| One more thing:select *,t1.amount-t.amount from (select ROW_NUMBER() over (order by (select (null)))rn ,amount from #tab)t inner join (select ROW_NUMBER() over (order by (select (null)))rn ,amount from #tab) t1 on t.rn = t1.rn+1Vijay is here to learn something from you guys. |
 |
|
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-05-30 : 14:24:41
|
| I'm confused as to how I add this column created from values on dbo.Data2 table to the table itself so it shows all 4 columns (Real, LossAmount, fx, dy) when I execute the code. Right now it's just displaying dy.I updated the code to look like this: (named the column "dy")with d as ( select LossAmount, seq = row_number() over (order by LossAmount desc) from dbo.Data2 ) select d.LossAmount - d2.LossAmount as dy from d d join d d2 on d2.seq = d.seq + 1 |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-30 : 14:37:10
|
quote: Originally posted by kotonikak I'm confused as to how I add this column created from values on dbo.Data2 table to the table itself so it shows all 4 columns (Real, LossAmount, fx, dy) when I execute the code. Right now it's just displaying dy.I updated the code to look like this: (named the column "dy")with d as ( select LossAmount, seq = row_number() over (order by LossAmount desc) from dbo.Data2 ) select d.LossAmount - d2.LossAmount as dy from d d join d d2 on d2.seq = d.seq + 1
Do this:with d as ( select Real, LossAmount, fx,seq = row_number() over (order by LossAmount desc) from dbo.Data2 ) select d.Real,d.LossAmount,d.fx, d.LossAmount - d2.LossAmount as dy from d d join d d2 on d2.seq = d.seq + 1 Vijay is here to learn something from you guys. |
 |
|
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-05-30 : 14:48:47
|
| thank you thank you! |
 |
|
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-06-06 : 09:54:07
|
| There's 20 rows in this table and one problem about this query is that when it gets to row 20 and wants to do the difference between row 20 and the next row (row 21 which doesn't exist), instead of just displaying NULL, the row (row 20) is dropped completely and my results only display 19 rows. Is there any way I can have it recognize this and just have it display a 0 on the last row (since there is no difference between row 20 and the nonexistent row 21)? i.e. get the following column:dy3,483 (6087-2605)297 (2605-2308) and so on30 508 710 346 3,754 914 3,001 568 1,115 427 2,518 1,767 1,030 3,148 203 3,355 2,549 0thanks! |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-06-06 : 10:19:01
|
| with d as ( select Real, LossAmount, fx,seq = row_number() over (order by LossAmount desc) from dbo.Data2 ) select d.Real,d.LossAmount,d.fx, coalesce(d.LossAmount - d2.LossAmount,0) as dy from d d left join d d2 on d2.seq = d.seq + 1==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
kotonikak
Yak Posting Veteran
92 Posts |
Posted - 2012-06-06 : 11:52:08
|
| thank you! |
 |
|
|
|