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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:49:59+00:00 2026-06-10T10:49:59+00:00

I have a conditional query as such: If a column (order) is not 0,

  • 0

I have a conditional query as such:

  • If a column (order) is not 0, order by value 1 to 99999 (max)
  • I need to order all 0s in order by last name.

So if I have a table as such

ID   Order     Last Name
1     0        Manner
2     1        Brock
3     0        Lester
4     0        Annual
5     0        Greatly

The results I expect are:

Brock
Annual
Greatly
Lester
Manner

Here is my query. What is happening every time is that I am getting last name sort, without the non-order 0 going first:

select c.last_name
from person_reports crt 
join person c 
where c.org_id = 1000 and crt.reports_to_id = 100389 and c.id = crt.contact_id 
order by c.last_name, case preference_num when 0 then 9999999 else preference_num end

Results of my current work:

Annual
Brock
Greatly
Lester
Manner

Thanks for any help

  • 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-10T10:50:01+00:00Added an answer on June 10, 2026 at 10:50 am

    You can simply reverse the ORDER BY clause to:

    order by case preference_num when 0 then 9999999 else preference_num end, c.last_name
    

    And you can avoid using the magic ‘9999999’ if you add one more segment at the beginning:

    order by case preference_num when 0 then 1 else 0 end, preference_num, c.last_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to use conditional select query in SQL Server ? I have a column
I have query table copyQuery which has Soil Condition has one of the columns,
I have following statement for query articles from some sections Article.all(:joins => :sections, :conditions
Is it possible for me to have a conditional in XSLT such that I
I have two tables: **Product** ID Name SKU **Brand** ID Name Product table has
I have a table in a database with one column containing dates and another
Trying to construct a query such that I have multiple statement specifying joins, each
I have a query to find all the phone numbers that match a partial
I need help writing a conditional where clause. here is my situation: I have
I have a table named coords with the following columns |name|lat|lon|und| import sqlite3 database

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.