July 13, 2010 at 6:54 pm
Can someone explain why when I add the WHERE statement, SQL Server returns the error 'Invalid column name Rn'? The first query works and I can see the result, but obviously somethings going on where SQL Server doesn't recognize it for the WHERE.
I've seen multiple examples similar to this that do appear to work and the syntax appears correct (http://msdn.microsoft.com/en-us/library/ms186734.aspx).
What am I missing?
SELECT *,ROW_NUMBER() OVER (PARTITION BY Comments.ShowNumber ORDER BY Comments.ShowNumber, Comments.Id) as Rn
FROM Comments
SELECT *,ROW_NUMBER() OVER (PARTITION BY Comments.ShowNumber ORDER BY Comments.ShowNumber, Comments.Id) as Rn
FROM Comments
WHERE Rn = 1
July 13, 2010 at 7:00 pm
david.c.holley (7/13/2010)
Can someone explain why when I add the WHERE statement, SQL Server returns the error 'Invalid column name Rn'? The first query works and I can see the result, but obviously somethings going on where SQL Server doesn't recognize it for the WHERE.I've seen multiple examples similar to this that do appear to work. What am I missing?
SELECT *,ROW_NUMBER() OVER (PARTITION BY Comments.ShowNumber ORDER BY Comments.ShowNumber, Comments.Id) as Rn
FROM Comments
SELECT *,ROW_NUMBER() OVER (PARTITION BY Comments.ShowNumber ORDER BY Comments.ShowNumber, Comments.Id) as Rn
FROM Comments
WHERE Rn = 1
Try this :
WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY Comments.ShowNumber ORDER BY Comments.ShowNumber, Comments.Id) as Rn
FROM Comments
)
select * from CTE WHERE Rn = 1
SQL Server assign the "RN" to the ROW_NUMBER only during execution.. so ur column name RN wont be present for the SQL server to use from the "FROM" clause..
July 13, 2010 at 9:27 pm
As per the execution of the SQL Statements that you have provided,
1. First the From clause will be processed
2. Then WHERE clause will be processed
3. Then SELECT clause will be processed
Since the SELECT clause is processed at last, any computation that is processed after WHERE clause cannot be referred before. This is the reason why your ROW_NUMBER computation is not available in WHERE clause processing.
For more information you can refer to T-SQL Querying book by Itzik-ben-gen.
July 13, 2010 at 10:22 pm
RakeshRSingh (7/13/2010)
As per the execution of the SQL Statements that you have provided,1. First the From clause will be processed
2. Then WHERE clause will be processed
3. Then SELECT clause will be processed
Since the SELECT clause is processed at last, any computation that is processed after WHERE clause cannot be referred before. This is the reason why your ROW_NUMBER computation is not available in WHERE clause processing.
For more information you can refer to T-SQL Querying book by Itzik-ben-gen.
Great explanation Rakesh. Pretty much spot on. My only objection would be that that's a very thick, fairly expensive book. You could at least provide a chapter number or page number. Since the book is also expensive, how about a suggestion as to what to look for in the FREE Books Online that comes with SQL Server? 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
July 14, 2010 at 10:02 am
RakeshRSingh (7/13/2010)
As per the execution of the SQL Statements that you have provided,1. First the From clause will be processed
2. Then WHERE clause will be processed
3. Then SELECT clause will be processed
Since the SELECT clause is processed at last, any computation that is processed after WHERE clause cannot be referred before. This is the reason why your ROW_NUMBER computation is not available in WHERE clause processing.
For more information you can refer to T-SQL Querying book by Itzik-ben-gen.
Which now that I read that and look back here, that makes sense with the examples provided. http://msdn.microsoft.com/en-us/library/ms186734.aspx
However, I would swear up and down that I've seen example where the value of the Row_Number is referenced in the WHERE statement without using WITH() AS.
July 14, 2010 at 10:46 am
david.c.holley (7/14/2010)
However, I would swear up and down that I've seen example where the value of the Row_Number is referenced in the WHERE statement without using WITH() AS.
It's possible you could have seen it like this:
SELECT * FROM
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY Comments.ShowNumber ORDER BY Comments.ShowNumber, Comments.Id) as Rn
FROM Comments
) sq
WHERE Rn = 1
(This is functionally exactly the same as the CTE structure that ColdCoffee provided above.)
July 14, 2010 at 5:53 pm
That's entirely a possibility.
It does also solve another question that I had that I've seen crop up here and there about selecting the oldest or youngest child record for a parent. I had run across an example and only remembered that Row_Number() was a part of the statement.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply