Wanting to learn. Who can best explain this in a simple yet crystal manner to a noob??

  • Hi all,

    I am very new to T-SQL.. and i came across this:

    ROW_NUMBER ( )

    OVER ( [ PARTITION BY value_expression , ... [ n ] ] order_by_clause )

    Can someone explain to me how this works, its purpose and when do we usually use it for?

    Am i asking for knowledge and i am very confident some1 from this excellent forum explain this well.

  • Its a ranking function, which gives a list of rownumbers based on what you detail in the partitioning and ordering clauses.

    The URL should explain it for you http://msdn.microsoft.com/en-us/library/ms186734.aspx

  • Thanks, yup i read that must still not as clear....

  • Can you explain which bits you are not clear on?

  • what does the partition does? and how does it affects the generating of row numbers. Does it have a similar function as GROUP BY?

  • The partition will restart the row number based on that you detail.

    So in the examples on the MSDN article, Example C uses partition, so that you can see the top person per region.

    So if you look at the output, you will see its not a sequential series of numbers in the row_number column, it goes 1,2,3,1,1,1,1,2,1,2,3,4 etc and you can see that it restarts the row number for reach region.

    So then the person can just say get me everyone where row number = 1 to get the top sales person for each region.

    There is no group by for ROW_NUMBER as it is a ranking function and will always generate a unique row so you wont be able to group the data, that should be done before using row number.

  • Oh ok thanks... i uds le;)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply