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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:45:03+00:00 2026-05-26T08:45:03+00:00

Here is the situation that I am trying to solve: I have a query

  • 0

Here is the situation that I am trying to solve:

I have a query that could return a set of records. The field being sorted by could have a number of different values – for the sake of this question we will say that the value could be A, B, C, D, E or Z

Now depending on the results of the query, the sorting needs to behave as follows:
If only A-E records are found then sorting them “naturally” is okay. But if a Z record is in the results, then it needs to be the first result in the query, but the rest of the records should be in “natural” sort order.

For instance, if A C D are found, then the result should be

A
C
D

But if A B D E Z are found then the result should be sorted:

Z
A
B
D
E

Currently, the query looks like:

SELECT NAME, SOME_OTHER_FIELDS FROM TABLE ORDER BY NAME

I know I can code a sort function to do what I want, but because of how I am using the results, I can’t seem to use because the results are being handled by a third party library, to which I am just passing the SQL query. It is then processing the results, and there seems to be no hooks for me to sort the results and just pass the results to the library. It needs to do the SQL query itself, and I have no access to the source code of the library.

So for all of you SQL gurus out there, can you provide a query for me that will do what I want?

  • 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-26T08:45:04+00:00Added an answer on May 26, 2026 at 8:45 am

    How do you identify the Z record? What sets it apart? Once you understand that, add it to your ORDER BY clause.

    SELECT name, *
    FROM [table]
    WHERE (x)
    ORDER BY
        (
         CASE
           WHEN (record matches Z) THEN 0
           ELSE 1
         END
        ),
        name
    

    This way, only the Z record will match the first ordering, and all other records will be sorted by the second-order sort (name). You can exclude the second-order sort if you really don’t need it.

    For example, if Z is the character string ‘Bob’, then your query might be:

    SELECT name, *
    FROM [table]
    WHERE (x)
    ORDER BY
        (
         CASE
           WHEN name='Bob' THEN 0
           ELSE 1
         END
        ), name
    

    My examples are for T-SQL, since you haven’t mentioned which database you’re using.

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

Sidebar

Related Questions

Here is situation I have been trying to solve Lets take a Employee table
Here's the situation I'm trying to solve: I have a main shared layout with
Here's my situation: I'm trying to create a SSL certificate that will be installed
Here is my situation: I have an application that use a configuration file. The
Here is my situation: I have one table that contains a list of drugs
Here's the situation: We have some generic graphics code that we use for one
Here is the situation. I have some javascript that looks like this: function onSubmit()
Here is the problem I'm trying to solve for my game. I have this
Here is the situation. I have a thread that gets started with the click
eeesh that title isn't very good. Here is the situation. I have a simulation

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.