Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to rank() by a particluar value

433185Oct 16 2005 — edited Oct 17 2005

HI,

i am trying to rank a column on the condition that a particular value exists

select * from t

id   col2	      	
1    1                         
2    17                        
3    1                           
4    1                           
5    11                           
6    1                           


i want to rank the values of col2 order by ID where col2 = 1. 
If col2 value is not 1 then rank column should show 0 

in other words, rank would show the progressive occurance of value = 1 in col2 order by ID column


the desired output

id   col2	   rank    	
1    1                 1         
2    17               0          
3    1                 2          
4    1                 3          
5    11               0          
6    1                 4          

pls tell how to achieve this

create table t
(id number,
col2 number)
/

insert into t values ( 1, 1)
insert into t values (2, 17)
insert into t values (3 , 1)
insert into t values (4 , 1)
insert into t values (5 , 11)
insert into t values (6 , 1)

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 14 2005
Added on Oct 16 2005
15 comments
835 views