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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:21:23+00:00 2026-05-26T02:21:23+00:00

I am trying to create a query that will shift the results on each

  • 0

I am trying to create a query that will shift the results on each execution for each user on my website. Goal to accomplish this using SQL Server 2008 database technology.

For example if I have a table with three rows of data:

I am trying to find a way to select this data order by name ascending then shifting/rotating the result.

ID  Name 
1   Apple 
2   Orange 
3   Banana

User 1 loads a page and sees

Apple
Banana
Orange

User 1 reloads the same page and then sees

Banana
Orange
Apple

User 1 reloads the same page and then sees

Orange
Apple
Banana

User 2, etc have the exact same experience, rotating/shifting of the results in the same order.

I can create and store any values necessary such as the first or last name or ID.

Is there a simple elegant solution (query, stored procedure, function, etc) that will accomplish this task at the SQL Server level?

The reason being is to give all colors the opportunity to be displayed at the top of the list vs alphabetical ordered A to Z.

  • 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-26T02:21:23+00:00Added an answer on May 26, 2026 at 2:21 am

    If SQL Server had sequences and a nextval() function like PostgreSQL or Oracle it could be as simple as that – given your base sort were on id, starting at 1, without holes:

    SELECT id, name
      FROM mytbl
     ORDER BY CASE WHEN id > @nextval%number_of_rows THEN 0 ELSE 1 END, rn
    

    See here for workarounds: Oracle sequence but then in MS SQL Server.

    Edit with new input from question

    It could look like this then:

    DECLARE @nextval int;
    
    WITH x AS
    (
    SELECT id
          ,name
          ,ROW_NUMBER() OVER(ORDER BY name) AS rn
          ,COUNT(*) OVER() AS ct
      FROM mytbl 
    )
    SELECT id
          ,name
      FROM x
     ORDER BY CASE WHEN rn > @nextval%ct THEN 0 ELSE 1 END, rn
    
    • Sort alphabetically.
    • Number of rows dynamic.
    • Syntax is tested now
    • You only need a way to increment @nextval. The above link may help.

    To get my syntax straight, I wrote a demo on data.stackexchange.com
    You can go there and check it out!

    This would be simpler if SQL Server had something like LIMIT and OFFSET. TOP can’t do the job. See: Equivalent of LIMIT and OFFSET for SQL Server?

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

Sidebar

Related Questions

I am trying to create a query that will show the details for each
I am trying to create a single query that will pull results from a
I'm trying to create an SQL query that will order the results by a
Trying to create a mysql query that will do the following: For each record
I am trying to create a report in SSRS2000 that will query an ORACLE
I'm trying to create a query that will pull the first 10 records of
I am trying to create an HQL query that will filter a tree based
I'm trying to create a query, that will calculate sum of products on invoice.
I'm trying to use LINQ To Objects to create a query that will give
I'm trying to create a query on SQL server 2005 that will check if

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.