Home > Enterprise >  Customize SQL queries to sorting
Customize SQL queries to sorting

Time:10-16

In my hierarchy_resoponse table have a values with ‘*’ and other values for the hierachy_id

I want to select hierarcy_response table record select hierarchy_id = ‘*’ records as first records and other records later.

Ex -: Records in my table -:

| response_id | hierarchy_id | hierarchy_name
| 1           | HI1          | Hierarchy 1
| 2           | *            | ABC Hierarchy
| 3           | *            | CD Hierarchy 
| 4           | Hie2         | Hierary 2

I want to select records as below -:

| response_id | hierarchy_id | hierarchy_name
| 2           | *            | ABC Hierarchy
| 3           | *            | CD Hierarchy 
| 1           | HI1          | Hierarchy 1
| 4           | Hie2         | Hierary 2

This is my select query -:

Select * from hierarchy_response;

How I modify this with ‘Order by’ query?

CodePudding user response:

Try with this,

Select * from hierarchy_response
ORDER BY CASE WHEN hierarchy_id = ‘*’ THEN 1
              ELSE 2 END;
  • Related