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 12»»

RANK() returns consecutive integers Expand / Collapse
Author
Message
Posted Wednesday, January 5, 2011 9:02 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 3:33 PM
Points: 4,388, Visits: 3,399
Comments posted to this topic are about the item RANK() returns consecutive integers
Post #1043461
Posted Thursday, January 6, 2011 2:09 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
There are several ranking functions:
RANK
DENSE_RANK
NTILE
ROW_NUMBER

When the question asked will it always return consecutive integers, I knew right away that it was false because none of them will always return consecutive integers if there is only one row. One row is still a non empty rowset but that was not explained in the provided explanation.
Post #1043553
Posted Thursday, January 6, 2011 2:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 AM
Points: 13,319, Visits: 10,184
Nice and easy. Thanks!



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1043556
Posted Thursday, January 6, 2011 2:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:20 PM
Points: 5,926, Visits: 8,175
cengland0 (1/6/2011)
When the question asked will it always return consecutive integers, I knew right away that it was false because none of them will always return consecutive integers if there is only one row.

Meh, I could just as well say that everything is consecutive in a single-row rowset.
According to http://www.thefreedictionary.com/consecutive, "consecutive" means (a.o.) "Following one after another without interruption" - and in a single row row-set, there can never be any interruption.

The reason that RANK does not return consecutive values is how ties are handled - for instance, if the RANK is based on the scores {9.6, 8.4, 9.6}, the results would be
Score Rank
9.6 1
9.6 1
8.4 3

Because two rows get the rank 1, the value 2 is skipped. That makes the values non-consecutive.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1043564
Posted Thursday, January 6, 2011 2:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
Hugo Kornelis (1/6/2011)
[quote]cengland0 (1/6/2011)

Meh, I could just as well say that everything is consecutive in a single-row rowset.
According to http://www.thefreedictionary.com/consecutive, "consecutive" means (a.o.) "Following one after another without interruption" - and in a single row row-set, there can never be any interruption.

You forgot a major component of the definition of consecutive. The "one after another" part. When you have a one row rowset, how can you have one after another in the resultset? That means you must have a minimum of two rows to have one after another.
Post #1043566
Posted Thursday, January 6, 2011 2:42 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 AM
Points: 13,319, Visits: 10,184
cengland0 (1/6/2011)
Hugo Kornelis (1/6/2011)
[quote]cengland0 (1/6/2011)

Meh, I could just as well say that everything is consecutive in a single-row rowset.
According to http://www.thefreedictionary.com/consecutive, "consecutive" means (a.o.) "Following one after another without interruption" - and in a single row row-set, there can never be any interruption.

You forgot a major component of the definition of consecutive. The "one after another" part. When you have a one row rowset, how can you have one after another in the resultset? That means you must have a minimum of two rows to have one after another.


If you really want to take it to a filosophical level, you could argue that one row comes after nothing.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1043568
Posted Thursday, January 6, 2011 4:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:04 PM
Points: 6,582, Visits: 8,861
cengland0 (1/6/2011)
There are several ranking functions:
RANK
DENSE_RANK
NTILE
ROW_NUMBER
Yes, there are several. However, this question is about the RANK() function, so why even bring these others into the mix?

When the question asked will it always return consecutive integers, I knew right away that it was false because none of them will always return consecutive integers if there is only one row. One row is still a non empty rowset but that was not explained in the provided explanation.


Are you just trying to find fault with the question?

A better argument would have been that all of the windowing functions accept a PARTITION BY clause, which re-starts the numbering - thus the results are not always sequential.

However, sometimes you just have to go with what you know the author meant to convey.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1043643
Posted Thursday, January 6, 2011 5:07 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
WayneS (1/6/2011)

Yes, there are several. However, this question is about the RANK() function, so why even bring these others into the mix?

Because I usually have to look them up to make sure I'm using the right one. But in this case, I didn't have to look anything up because none of them would return consecutive integers (when there is a one-row result).

Are you just trying to find fault with the question?


Not necessarily. I do work for a very large company, in the training organization, and would consider myself an expert on quizzes.

There have been cases of companies firing employees during training for failing tests. To avoid legal issues, you better look at each question carefully to make sure the answer they selected can never be a correct answer. For this reason, I am always looking at these QOTD's with a different eye than a regular person.

In a previous company, I have been involved in the process of firing and disciplinary actions of employees not passing tests. I've also had to gather supporting documentation to take to court when an ex-employee took legal action.
Post #1043658
Posted Thursday, January 6, 2011 5:16 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
Another way to put this is if the question said:

In a non-empty rowset, ROW_NUMBER() always returns consecutive integers.

What would you answer? True or False?

I'd still answer False because it's possible to have a one-row rowset but the author is probably thinking True.

I did get the answer for this QOTD correct.
Post #1043668
Posted Thursday, January 6, 2011 7:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:20 PM
Points: 5,926, Visits: 8,175
cengland0 (1/6/2011)
Another way to put this is if the question said:

In a non-empty rowset, ROW_NUMBER() always returns consecutive integers.

What would you answer? True or False?

I'd still answer False because it's possible to have a one-row rowset but the author is probably thinking True.

I guess I'd first check to see if you are the author of that question ;)

But had I seen such a question before today, then I think there's about 80% chance I'd have replied True.
If the correct answer had been False because of PARTITION BY, I would have slapped myself on the forehead and complimented the question's author.
If the correct answer had been False because of a single-row rowset "not being consecutive", I would have felt misled and cheated.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1043748
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse