SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Window Functions for Row Number


Window Functions for Row Number

Author
Message
Jeff Atherton
Jeff Atherton
SSC Eights!
SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)

Group: General Forum Members
Points: 970 Visits: 438
Comments posted to this topic are about the item Window Functions for Row Number
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8933 Visits: 7281
Nice easy question, thanks Jeff

However, there appears to be an inaccuracy in the answer, viz:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and OVER clause
i believe should read:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and ORDER BY clauses


____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5908 Visits: 5080
Stewart "Arturius" Campbell - Thursday, April 20, 2017 11:10 PM
Nice easy question, thanks Jeff

However, there appears to be an inaccuracy in the answer, viz:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and OVER clause
i believe should read:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and ORDER BY clauses

From the BOL, both of these are correct

SELECT
ROW_NUMBER() OVER(PARTITION BY recovery_model_desc ORDER BY name ASC) AS Row#,
name, recovery_model_desc
FROM sys.databases WHERE database_id < 5;

SELECT
ROW_NUMBER() OVER( ORDER BY name ASC) AS Row#,
name, recovery_model_desc
FROM sys.databases WHERE database_id < 5;

The answer of the QotD should be corrected.


Igor Micev,
SQL Server developer at Seavus
My blog: www.igormicev.com
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87595 Visits: 45272
Igor Micev - Friday, April 21, 2017 1:59 AM
Stewart "Arturius" Campbell - Thursday, April 20, 2017 11:10 PM
Nice easy question, thanks Jeff

However, there appears to be an inaccuracy in the answer, viz:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and OVER clause
i believe should read:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and ORDER BY clauses

From the BOL, both of these are correct

SELECT
ROW_NUMBER() OVER(PARTITION BY recovery_model_desc ORDER BY name ASC) AS Row#,
name, recovery_model_desc
FROM sys.databases WHERE database_id < 5;

SELECT
ROW_NUMBER() OVER( ORDER BY name ASC) AS Row#,
name, recovery_model_desc
FROM sys.databases WHERE database_id < 5;

The answer of the QotD should be corrected.

The QoD is correct as it stands.
ROW_NUMBER() OVER(PARTITION BY recovery_model_desc ORDER BY name ASC) AS Row# is syntatically valid, but was not one of the answers offered. The answer that was offered with both partition by and order by in had a comma somewhere that it does not belong.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Neil Burton
Neil Burton
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3414 Visits: 10172
Igor Micev - Friday, April 21, 2017 1:59 AM
Stewart "Arturius" Campbell - Thursday, April 20, 2017 11:10 PM
Nice easy question, thanks Jeff

However, there appears to be an inaccuracy in the answer, viz:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and OVER clause
i believe should read:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and ORDER BY clauses

From the BOL, both of these are correct

SELECT
ROW_NUMBER() OVER(PARTITION BY recovery_model_desc ORDER BY name ASC) AS Row#,
name, recovery_model_desc
FROM sys.databases WHERE database_id < 5;

SELECT
ROW_NUMBER() OVER( ORDER BY name ASC) AS Row#,
name, recovery_model_desc
FROM sys.databases WHERE database_id < 5;

The answer of the QotD should be corrected.

You're right in what you say Igor but if you look very carefully there's a comma between the PARTITION BY and the ORDER BY in one of the incorrect answers. As soon as you try to run the incorrect option it throws an error at you. It nearly caught me out because at an under-caffeinated first glance they both appeared correct.



On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
—Charles Babbage, Passages from the Life of a Philosopher

How to post a question to get the most help
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87595 Visits: 45272
Neil Burton - Friday, April 21, 2017 2:17 AM

You're right in what you say Igor but if you look very carefully there's a comma between the PARTITION BY and the ORDER BY in one of the incorrect answers. As soon as you try to run the incorrect option it throws an error at you. It nearly caught me out because at an under-caffeinated first glance they both appeared correct.


I spotted it, because it's a type of syntax error I make quite often. For some reason when writing window functions, I tend to put a comma in the OVER clause if I'm not paying attention.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Ed Wagner
Ed Wagner
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: 16671 Visits: 10070
GilaMonster - Friday, April 21, 2017 3:08 AM
Neil Burton - Friday, April 21, 2017 2:17 AM

You're right in what you say Igor but if you look very carefully there's a comma between the PARTITION BY and the ORDER BY in one of the incorrect answers. As soon as you try to run the incorrect option it throws an error at you. It nearly caught me out because at an under-caffeinated first glance they both appeared correct.


I spotted it, because it's a type of syntax error I make quite often. For some reason when writing window functions, I tend to put a comma in the OVER clause if I'm not paying attention.

Nice and simple question to end the week. The comma jumped out at me because I've done in more times than I'd like to admit. I can't blame intellisence for it either. Wink



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
sestell1
sestell1
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2547 Visits: 3462
GilaMonster - Friday, April 21, 2017 3:08 AM
Neil Burton - Friday, April 21, 2017 2:17 AM

You're right in what you say Igor but if you look very carefully there's a comma between the PARTITION BY and the ORDER BY in one of the incorrect answers. As soon as you try to run the incorrect option it throws an error at you. It nearly caught me out because at an under-caffeinated first glance they both appeared correct.


I spotted it, because it's a type of syntax error I make quite often. For some reason when writing window functions, I tend to put a comma in the OVER clause if I'm not paying attention.

Nice to know I'm not the only one that seems to instinctively add a comma for some reason. Smile

Jeff Atherton
Jeff Atherton
SSC Eights!
SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)

Group: General Forum Members
Points: 970 Visits: 438
Stewart "Arturius" Campbell - Thursday, April 20, 2017 11:10 PM
Nice easy question, thanks Jeff

However, there appears to be an inaccuracy in the answer, viz:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and OVER clause
i believe should read:
if a PARTITION BY clause is used there is no comma between the PARTITION BY and ORDER BY clauses


Nice catch Stewart. Looks like once the question is posted I can no longer edit. So maybe some super admin on this site can update that.
Jeff Atherton
Jeff Atherton
SSC Eights!
SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)

Group: General Forum Members
Points: 970 Visits: 438
GilaMonster - Friday, April 21, 2017 3:08 AM


I spotted it, because it's a type of syntax error I make quite often. For some reason when writing window functions, I tend to put a comma in the OVER clause if I'm not paying attention.


Same here. I always want to add a comma in the windows functions and then with Nested CTEs I always seem to forget the comma.
Revenant
Revenant
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7361 Visits: 4863
Jeff Atherton - Friday, April 21, 2017 7:08 AM
GilaMonster - Friday, April 21, 2017 3:08 AM


I spotted it, because it's a type of syntax error I make quite often. For some reason when writing window functions, I tend to put a comma in the OVER clause if I'm not paying attention.


Same here. I always want to add a comma in the windows functions and then with Nested CTEs I always seem to forget the comma.

Me, too. Thanks for the question, Jeff!
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