Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 376431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:35:54+00:00 2026-05-12T14:35:54+00:00

Hoping this is trivial for a SQL-Ninja… Been trying to get the following query

  • 0

Hoping this is trivial for a SQL-Ninja… Been trying to get the following query working:

This is for SQL Server 2008

SELECT 
    ROW_NUMBER() OVER (ORDER BY Date_Time DESC) AS RowNumber, *
FROM
    (SELECT 
         T.A_ID, T.User_Name, T.Date_Time, T.Value,
         U.ID, U.Name, U.Field1, U.Field2,
         COUNT(U.ID) OVER () AS TotalRows
     FROM 
        TeeTable as T 
    INNER JOIN 
        YouTable AS U ON T.U_ID = U.ID
    WHERE 
        T.Value BETWEEN 222 AND 225) Filtered
WHERE 
    RowNumber BETWEEN 1 AND 5

The values are somewhat contrived to give a specific example, but the spirit of the query is completely preserved. The error I get from this statement is:

Invalid column name ‘RowNumber’.

If I remove the final WHERE clause (RowNumber BETWEEN …) it returns an expected result set (A_ID, User_Name, Date_Time etc…), with RowNumber as a column (with sensical values) in said results. Yet I cannot compare against it in the WHERE clause. I’m clearly doing something stupid but this hits my SQL limit!

I’ve tried re-arranging this as a CTE as well, (WITH Filtered AS …) but the end result is the same, it appears that’s just a sugar for what I’m already doing anyway.

Ideas? How can I filter against the RowNumber derived column?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:35:54+00:00Added an answer on May 12, 2026 at 2:35 pm

    You must move the WHERE operator above the project list where RowNumber column is created. Use a derived table or a CTE:

    SELECT * 
      FROM (
       SELECT *, ROW_NUMBER() OVER (...) as RowNumber
       FROM ...) As ...
     WHERE RowNumber = ...
    

    the equivalent CTE is:

    WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (...) as RowNumber
           FROM ...)
    SELECT * FROM cte 
    WHERE RowNumber = ...   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hoping this will be an easy one :) I've been stuffing around for
Obviously this is trivial to do with win32 api - CreateDirectory(). But I'm trying
So, I've been working for some time on connecting hashing out a trivial application,
Okay, this may be trivial, but I'm hoping somebody can give me a straightforward
Hoping this question will get answered. Basically I am attempting to open an executable
Hoping this is just a case of syntax. I'm writing a custom search function
I'm hoping this is something silly I've done. I got a function unigref near
Good afternoon I'm hoping this is going to be nice and a simple f**k
I'm hoping this just needs a new pair of eyes casting over it. I
I'm hoping this question has a very simple answer. I can think of ways

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.