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 7978045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:22:58+00:00 2026-06-04T09:22:58+00:00

I have the following select SQL that does a basic select statement although it

  • 0

I have the following select SQL that does a basic select statement although it does include a calculated column:

    Select *
    From
    (
      Select *,
             ROW_NUMBER() OVER
                              (ORDER BY
                                CASE WHEN @sortBy = 0 THEN R.DateCreated End Desc,
                                CASE WHEN @sortBy = 1 THEN R.DateCreated end Asc,
                                CASE WHEN @sortBy = 2 THEN TotalVotes END Desc,
                                CASE WHEN @sortBy = 2 THEN R.TotalFoundNotUseful END Desc
                              ) AS RowNumber 

      From
      (
        Select *, (TotalFoundUseful + TotalFoundNotUseful) As TotalVotes
      From Reviews
      Where (DealID = @dealID) And (TotalAbuses < 10) And (Deleted = 0)
    ) As R
  ) As Rev
  Where RowNumber BETWEEN @startRecord AND @endRecord

If you look carefully, the SELECT statement itself is executed 3 times. I can’t believe that this is necessary. Is there a way to reduce this to 2 select statements (or possibly even one). I don’t actually need to return the RowNumber. It is only used for selecting rows within a certain range.

  • 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-04T09:23:00+00:00Added an answer on June 4, 2026 at 9:23 am

    You can do it with two by putting the rownumber in with the original select against reviews. You can’t go to one if you want to have a WHERE clause on a windowing function like ROW_NUMBER.

    You do have to write TotalFoundUseful + TotalFoundNotUseful twice but it will only be evaluated once so that doesn’t effect performance.

    I also wouldn’t expect moving to two to have any effect on performance but you should test it.

    Select *
        From
        (
      Select *, (TotalFoundUseful + TotalFoundNotUseful) As TotalVotes,
              ROW_NUMBER() OVER
                  (ORDER BY
                CASE WHEN @sortBy = 0 THEN DateCreated End Desc,
                CASE WHEN @sortBy = 1 THEN DateCreated end Asc,
                CASE WHEN @sortBy = 2 THEN TotalFoundUseful + TotalFoundNotUseful END Desc,
                CASE WHEN @sortBy = 2 THEN TotalFoundUseful END Desc
              ) AS RowNumber 
     From Reviews
         Where (DealID = @dealID) And (TotalAbuses < 10) And (Deleted = 0)
    
      ) As Rev
      Where RowNumber BETWEEN @startRecord AND @endRecord
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to following SQL Statement that I want to conver to LINQ Select
I have an MS SQL function that is called with the following syntax: SELECT
I have the following SQL: SELECT * FROM [Database].dbo.[TagsPerItem] INNER JOIN [Database].dbo.[Tag] ON [Tag].Id
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as
I have the following SQL statement SELECT c.CorpSystemID, c.SystemName , case when a.TaskItemID is
So I have the following group by SQL select count(*) as NO_OF_MSGS,FROM_USER,PROFILE_IMG,MSG from MESSAGES
I have the following sql statement that I need to make quicker. There are
I have the following piece of SQL that does not work: declare @id INT;
I have the following sql query SELECT * FROM jos_jcalpro_events AS e LEFT JOIN

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.