jwebster
jwebster

Reputation: 648

SQL Sorting within a sort

Okay, this should be a fairly easy question I think, but haven't been able to figure it out yet.

I have a table entitled Staff. The staff DESC is below:

SQL> DESC Staff
Name                                      Null?    Type
----------------------------------------- -------- ----------
SNO                                       NOT NULL CHAR(5)
FNAME                                              VARCHAR2(3
LNAME                                              VARCHAR2(3
POSITION                                           VARCHAR2(1
SEX                                                CHAR(1)
DOB                                                DATE
SALARY                                             NUMBER(9)
BNO                                                CHAR(4)

Here is the current query I am using:

SELECT bNo, lName||' '||fName, position, salary
FROM Staff
WHERE bNo 'b009'
OR bNo = 'b005'
ORDER BY 
    case position
        when 'Manager' then 1
        when 'Secretary' then 2
        when 'Supervisor' then 3
        when 'Assistant' then 4
    else 99
end;

The problem I am having is that within each position group, I need to sort by lName as well. Any tips would be greatly appreciated. Thanks in advance.

Upvotes: 2

Views: 432

Answers (1)

Martin Smith
Martin Smith

Reputation: 453287

Just add lName as a second ORDER BY column.

ORDER  BY CASE position
            WHEN 'Manager' THEN 1
            WHEN 'Secretary' THEN 2
            WHEN 'Supervisor' THEN 3
            WHEN 'Assistant' THEN 4
            ELSE 99
          END,
          lName; 

Upvotes: 6

Related Questions