Friday, February 15, 2008

Rank Row

One of the developer came to me yesterday. she was asking "I have some duplicate rows in col1 but not in col2 how can i just want to display the first row?


Here is the sample table so you can imagine it

me@d012band> select cust_no, name1 from vd.t2;

CUST_NO NAME1
---------------- --------
0000000900000377 ABC 1
0000000900000377 ABC 2
0000000900000377 ABC 3
0000000900001059 DEF 1
0000000900001059 DEF 2
0000000900001059 DEF 3
0000000900001059 BEA 1
0000000900003125 WER 1
0000000900003125 WER 2
0000000900003125 WER 3
0000000900004777 AFG 1
0000000900004777 AFG 2
0000000900004777 AFG 3

13 rows selected.


the output that she wanted to is something like this


CUST_NO NAME1
---------------- --------
0000000900000377 ABC 1
0000000900001059 DEF 1
0000000900003125 WER 1
0000000900004777 AFG 1


So, what i did is run this query and bingo we got the answer she wanted to


me@d012band> select cust_no, name1 from
2 (select cust_no, name1,rank() over (partition by cust_no order by rowid) rank_n from vd.t2)
3 where rank_n = 1;


CUST_NO NAME1
---------------- --------
0000000900000377 ABC 1
0000000900001059 DEF 1
0000000900003125 WER 1
0000000900004777 AFG 1



I used rank() function to rank every row, thanks to Oracle that build this kind function so that can make our query easier


Have a nice day


FZheng

2 comments:

Anonymous said...

hello sir,
please advise how can i move forward with the following requirement.
Thanks.
1- 2-node RAC Cluster
2- Version is 10gR1 Standard Edition on both Nodes with ASM.
Customer is providing a new hardware setup having new high speed disks, 10g R2 Enterprise Edition with ASM and 64-bit windows server 2003.


Now i need to migrate to the new hardware with different hardware,windows version,Oracle Version and new disks as well.

action plan that i have in my mind.

1- install 32-bit enteprise edition 10gR2 on existing system and migrate from 10gR1 to 10gR2 first.
2- how should i migrate datafiles in ASM? 10gR1 does not have asmcmd.

please reply me at user1603254@gmail.com
thanks.

oracle r12 documentation said...

I like your blog but I don't understand the question asked in this post. So I can't even try for the answer myself. I want you to better explain it with a example so that I can understand the logic behind it. I try working it out. Thanks.