tags:

views:

283

answers:

2

I have a table that's like this

rank continuationofrow
1    row 
2    row 
3    row 
4    row 
4    row  
4    row

I'm trying to identify the previous rows rank number within an Oracle statement. Any help is greatly appreciated. I've searched the internet and haven't found much.

+1  A: 
select
  ...
 lag(rank, 1) over (order by ordering-columns)
from
  ..
René Nyffenegger
+1  A: 

You must have another column that establishes the order of the rows with the same rank, otherwise the concept of "previous row" is meaningless. Let's suppose you do:

seq rank continuationofrow
1   1    row 
2   2    row 
3   3    row 
4   4    row 
5   4    row  
6   4    row

No you can use an analytic function:

select seq, rank, continuationofrow, lag(rank) over (order by seq) as prev_rank
from mytable;


seq rank continuationofrow prev_rank
1   1    row  
2   2    row               1
3   3    row               2
4   4    row               3
5   4    row               4
6   4    row               4
Tony Andrews