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
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1948 Visits: 502
Comments posted to this topic are about the item Window Functions for Row Number
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35522 Visits: 7702
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
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19782 Visits: 5191
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,
My blog: www.igormicev.com
GilaMonster
GilaMonster
SSC Guru
SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)

Group: General Forum Members
Points: 552187 Visits: 47744
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
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12168 Visits: 11434
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 (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)SSC Guru (552K reputation)

Group: General Forum Members
Points: 552187 Visits: 47744
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
SSC Guru
SSC Guru (157K reputation)SSC Guru (157K reputation)SSC Guru (157K reputation)SSC Guru (157K reputation)SSC Guru (157K reputation)SSC Guru (157K reputation)SSC Guru (157K reputation)SSC Guru (157K reputation)

Group: General Forum Members
Points: 157739 Visits: 11663
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
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6066 Visits: 3688
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
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1948 Visits: 502
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
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1948 Visits: 502
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
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22519 Visits: 5136
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!
hakan.winther
hakan.winther
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4143 Visits: 615
I almost got it wrong as I didn't see the comma first and got confused so I had to read the options twice. Sometimes the devil is in the details.

/Håkan Winther
MCITPBigGrinatabase Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
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