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

  • Home
  • SEARCH
  • 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 9179183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:44:12+00:00 2026-06-17T17:44:12+00:00

I have a SQL statement which is being used to page the results using

  • 0

I have a SQL statement which is being used to page the results using ROW_NUMBER()

SELECT * FROM (
    SELECT
        ROW_NUMBER() OVER ( ORDER BY [tbl1].[Col1] ASC ) As RowNumber,
        [tbl1].[Col1],
        [tbl1].[Col2],
        [tbl1].[Col3],
        ( SELECT [tbl2].[Col1] 
          FROM [tbl2] 
          WHERE [tbl2].[id] = [tbl1].[id] 
                AND [tbl2].[subid] = 2 ) As theColumnName
    FROM
        [tbl1]
) As tbl
WHERE RowNumber BETWEEN 1 AND 10

This all works fine and as expected. However I now want to introduce some additional ORDER BY functionality based on a value passed into the stored procedure.

So I have updated my ROW_NUMBER() OVER (ORDER BY... statement to

ROW_NUMBER() OVER (ORDER BY 
    CASE WHEN @sortBy = 'type1' THEN [tbl1].[Col1]
         WHEN @sortBy = 'type2' THEN [tbl1].[Col2]
    END,
    CASE WHEN @sortBy = 'type3' THEN [tbl1].[Col3]
    END
ASC) As RowNumber

As you can see here I have a CASE statement within the OVER () method, and there are two CASE‘s because [Col3] is a different data type than the other colums.

Now this works fine, but I cannot find out how to order by my column theColumnName as I just receive the error message Invalid column name 'theColumnName'.

WHEN @sortBy = 'type4' THEN theColumnName

I have even tried to use the column number, e.g. WHEN @sortBy = 'type4' THEN 5 but this will not work either.

Is there a way to Order By this custom column name?

  • 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-06-17T17:44:14+00:00Added an answer on June 17, 2026 at 5:44 pm

    You need to move the subquery to the from clause, such as:

    SELECT
        ROW_NUMBER() OVER ( ORDER BY [tbl1].[Col1] ASC ) As RowNumber,
        [tbl1].[Col1],
        [tbl1].[Col2],
        [tbl1].[Col3],
        t2.col1 As theColumnName
    FROM [tbl1] t join
         (select id, col1
          from tbl2
          where subid = 2
        ) t2
        on t.id = t2.id
    

    Now, you will be able to include t2.col1 in the row_number() order by clause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL statement which has a very bad performance: SELECT frmInstLastModifiedDate
I have sql statement one; select linkscat.id, linkscat.category from linkscat, contentlinks, links where contentlinks.linksid
I have this SQL statement: SELECT * FROM history WHERE (fk_person = 2119) AND
I have a SQL statement like the following: select A from table1, (select B
I have a SQL statement which is collating lots of info from several tables
I have a SQL statement which returns a number of rows and these are
I have a fairly large SQL statement which has a number of inner joins
I have a method which includes sql statement . it is public Boolean addRSS(string
I have 3 tables which need to be linked in an SQL statement (I'm
I currently have this statement in my SQL View (SQL Server 2008 R2) which

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.