How to distinguish between the first and rest for duplicate records using sql ?

Posted by Biswanath on Stack Overflow See other posts from Stack Overflow or by Biswanath
Published on 2010-06-01T09:10:01Z Indexed on 2010/06/01 9:13 UTC
Read the original article Hit count: 164

Filed under:

Hi,

These are the input table and required output table.


                                  Input table 

ID  Name 
-------------
1   aaa
1   ababaa
2   bbbbbb
2   bcbcbccbc
2   bcdbcdbbbbb
3   ccccc

                                 Output table 

ID  Name        Ord
-----------------------------
1   aaa         first
1   ababaa      rest    
2   bbbbbb      first   
2   bcbcbccbc   rest    
2   bcdbcdbbbbb rest
3   ccccc       first

First and Rest is based on the occurrence of an ID field.

Is there a way to write a SQL query to achieve this ?

P.S. - This question is somewhat similar to what I am looking for.

© Stack Overflow or respective owner

Related posts about sql