Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


RANK() returns consecutive integers


RANK() returns consecutive integers

Author
Message
Revenant
Revenant
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5813 Visits: 4718
Comments posted to this topic are about the item RANK() returns consecutive integers
cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
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.
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16469 Visits: 13207
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 SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8337 Visits: 11580
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
cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
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.
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16469 Visits: 13207
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 SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
WayneS
WayneS
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6243 Visits: 10403
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
Author - SQL Server T-SQL Recipes
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

cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
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.
cengland0
cengland0
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1484 Visits: 1300
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.
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8337 Visits: 11580
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 Wink

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search