January 5, 2011 at 9:02 pm
Comments posted to this topic are about the item RANK() returns consecutive integers
January 6, 2011 at 2:09 am
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.
January 6, 2011 at 2:12 am
Nice and easy. Thanks!
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
January 6, 2011 at 2:32 am
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.
January 6, 2011 at 2:38 am
Hugo Kornelis (1/6/2011)
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.
January 6, 2011 at 2:42 am
cengland0 (1/6/2011)
Hugo Kornelis (1/6/2011)
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.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
January 6, 2011 at 4:50 am
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
January 6, 2011 at 5:07 am
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.
January 6, 2011 at 5:16 am
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.
January 6, 2011 at 7:32 am
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.
January 6, 2011 at 9:16 am
cengland0 (1/6/2011)
There are several ranking functions:. . . One row is still a non empty rowset but that was not explained in the provided explanation.
You are absolutely right. Thank you for pointing out this boundary case.
In my defense, I am still doing better than SQLS BOL which are not mentioning an empty set.
😉
January 6, 2011 at 10:49 am
Thanks for the question!
January 6, 2011 at 11:33 am
Thanks for the question.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 6, 2011 at 12:37 pm
Is it considered cheating that I went to the BOL before answering the question and read "If two or more rows tie for a rank, each tied rows receives the same rank."?
______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
January 6, 2011 at 1:54 pm
toddasd (1/6/2011)
Is it considered cheating that I went to the BOL before answering the question and read "If two or more rows tie for a rank, each tied rows receives the same rank."?
I don't consider doing research and learning from it cheating.
I'm not going to memorize every feature, trace flag, syntax, dmv of SQL Server. If I need something, I'll look it up 🙂
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply