开发者

How do I compare rows in an Oracle Table?

I h开发者_Go百科ave 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.


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


select
  ...
 lag(rank, 1) over (order by ordering-columns)
from
  ..
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜