Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Wanting to learn. Who can best explain this in a simple yet crystal manner to a noob?? Expand / Collapse
Author
Message
Posted Wednesday, January 9, 2013 2:39 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, February 2, 2013 11:09 AM
Points: 97, Visits: 180
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.
Post #1404619
Posted Wednesday, January 9, 2013 2:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:52 AM
Points: 5,131, Visits: 4,918
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




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404620
Posted Wednesday, January 9, 2013 2:58 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, February 2, 2013 11:09 AM
Points: 97, Visits: 180
Thanks, yup i read that must still not as clear....
Post #1404627
Posted Wednesday, January 9, 2013 2:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:52 AM
Points: 5,131, Visits: 4,918
Can you explain which bits you are not clear on?




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404628
Posted Wednesday, January 9, 2013 3:04 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, February 2, 2013 11:09 AM
Points: 97, Visits: 180
what does the partition does? and how does it affects the generating of row numbers. Does it have a similar function as GROUP BY?
Post #1404633
Posted Wednesday, January 9, 2013 3:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:52 AM
Points: 5,131, Visits: 4,918
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.




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1404640
Posted Wednesday, January 9, 2013 3:27 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, February 2, 2013 11:09 AM
Points: 97, Visits: 180
Oh ok thanks... i uds le;)
Post #1404646
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse