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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:32:02+00:00 2026-06-02T14:32:02+00:00

I need help with SQL (MSSQL), where I am sorting by multiple boolean types

  • 0

I need help with SQL (MSSQL), where I am sorting by multiple boolean types in order to group the results, but also need to sort by a date field within each of the type groupings.

The following SQL seemingly works ok:

Select * from staff ORDER BY admin DESC, hr DESC, sales DESC, it DESC, updated DESC

The trick is that some records have more than 1 type selected and then the output gets messed up. I only need to show staff in one grouping, but they should appear sorted by updated DESC within each grouping.

This is the output I get:

Name  updated    admin  hr     sales  it   
-------------------------------------------
fred  2012/04/01 true   true
bill  2011/10/01 true   true
joe   2012/04/01 true
sam   2012/03/01 true
jo    2012/02/01 true
beth  2012/03/01        true
mary  2012/02/01        true
harry 2011/02/01               true  true
gary  2012/04/01               true
bruce 2012/04/01                     true

This is the output I need

Name  updated    admin  hr     sales  it   
-------------------------------------------
fred  2012/04/01 true   true
joe   2012/04/01 true
sam   2012/03/01 true
jo    2012/02/01 true
bill  2011/10/01 true   true
beth  2012/03/01        true
mary  2012/02/01        true
gary  2012/04/01               true
harry 2011/02/01               true  true
bruce 2012/04/01                     true

Hope that makes sense.

  • 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-02T14:32:07+00:00Added an answer on June 2, 2026 at 2:32 pm

    You can use case expressions in the order by clause for conditional sorting.

    SELECT *
    FROM staff
    ORDER BY
        admin DESC,
        CASE admin WHEN 'true' THEN updated END DESC,
        hr DESC,
        CASE hr WHEN 'true' THEN updated END DESC,
        sales DESC,
        CASE sales WHEN 'true' THEN updated END DESC,
        it DESC,
        updated DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with the SQL ORDER BY clause. In my table below I
Okay, I need help. I'm usually pretty good at SQL queries but this one
I need some help sorting my score value's of my sql database <table border
need help/guide for sql select query, I have 2 table stock and stock_history, in
I need some help with SQL Query. I am trying to select all records
I need help with improving the performance of the following SQL query. The database
I'm somewhat new to SQL and need help with query syntax. My issue involves
I have an interesting SQL problem that I need help with. Here is the
We are using Linq-to-SQL with stored Procedures we need help on one of our
I'm not good at t-sql, so I need help. I have this code I

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.