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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:19:51+00:00 2026-06-19T04:19:51+00:00

I need to query a table and get the records back in a complex

  • 0

I need to query a table and get the records back in a complex scheme of ordering.

I can get the results I want with 3 separate queries and then concatenate the results by brute force after retrieval with my programming code.

I would rather get everything back with one call to the database, with one database.

I have a table called “committee” listing committee names and subcommittee names.

committee_name               subcomittee_name
--------------               -----------------
Hals Committee on Books
Hals Committee on Books      Subcommittee on Paperbacks
Hals Committee on Books      Subcommittee on Leather Bound Volumes
General Purpose Committee
Stans Committee on Music
Stans Committee on Music     Subcommittee on CDs
Stans Committee on Music     Subcommittee on MP3s

Every subcommittee belongs to a parent committee.

Not all committees have a subcommittee.

Every committee will have a null entry for subcommittee to distinguish a new/different committee

I would like to get all of the committee and subcommittee names back, sorted in ascending order, but with a twist. I would like all of “Stans” committees listed first, then Hals and then the others.

Example:

committee_name               subcomittee_name
--------------               -----------------
Stans Committee on Music
Stans Committee on Music     Subcommittee on CDs
Stans Committee on Music     Subcommittee on MP3s
Hals Committee on Books
Hals Committee on Books      Subcommittee on Paperbacks
Hals Committee on Books      Subcommittee on Leather Bound Volumes
General Purpose Committee

I can get that with 3 separate queries:

select * from committee where substr(committee_name,1,1) = 'S' order by committee_name,subcommittee_name nulls first

select * from committee where substr(committee_name,1,1) = 'H' order by committee_name,subcommittee_name nulls first

select * from committee where substr(committee_name,1,1) not in ('H','S') order by committee_name,subcommittee_name nulls first

What I would like is to get what I want with just 1 query, 1 trip to the database.

Any ideas?

Steve

  • 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-19T04:19:53+00:00Added an answer on June 19, 2026 at 4:19 am

    You could use a case to order ‘S’ first, then ‘H’, and then other letters:

    select  committee_name
    ,       subcommittee_name 
    from    committee 
    group by 
            committee_name
    ,       subcommittee_name
    order by
            case min(substring(comittee_name,1,1))
            when 'S' then 1
            when 'H' then 2
            else 3
            end
    ,       committee_name
    ,       subcommittee_name nulls first
    

    The min aggregate is required because you are potentially sorting a whole group of rows.

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

Sidebar

Related Questions

I need a query which can do the following operation. I have a table
I need to make a query output Using table, I want the following report,
I have 2 tables, and want to get records from 1 table and to
Possible Duplicate: Select Records multiple times from table I want to have my query
I want to get 1000 records from a table randomly, so I use: SELECT
I need to get a set of distinct records for a table along with
I have a database where I need to query to get records from one
I need to get data from several tables, so I used a query with
I have three table I need to query table 1,2 and 3 and select
I need a query that will return a table where each column is the

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.