Tuesday, February 7, 2012

DENSE_RANK()

My requirement is to order the contents of my selected SQL query. I can use ORDER BY for fulfill this task, but the problem is it will order my entire SQL query with ascending order or descending order according the way of using it. But my requirement is order the separate sub groups of my selected query. 

Let take this example. 
I want to order the each and every employees dependents, using their birthdays in ascending order with partition by the employee number. 


So I'm using the below SQL command. 

DENSE_RANK() OVER (PARTITION BY EMP_NUMBER ORDER BY DOB)
DENSE_RANK() OVER (PARTITION BY < PARTITION FIELD NAME> ORDER BY <ORDER FIELD NAME>)

Finally my result came as 


No comments:

Post a Comment