SQL - Specified Alphabetical Order

I need a specific order for the Completion_Status column, as in:

Completed, passed, not completed and not completed

How should I do it? I want to determine the order above.

I tried CASE, but it gave me an error, as in expecting NUM not CHAR.

select DISTINCT
  u.first_name || ' ' ||  u.last_name  "Employee_Name",
  rco.title "Course_Name",
  decode(p.status,'P','Passed', 'F','Failed', 'C','Completed', 'I','Incomplete', 'Not Attempted') "Completion_Status",

   to_char(p.completed_date,'YYYY-MM-DD') "Date_Completed"
   from
   offering o, offering_enrollment oe, users u ,  performance p, offering_content_object c, content_object rco
  where
  o.id = oe.offering_id  and
  u.id = oe.user_id and
  o.content_object_id = c.id AND
  p.content_object_id = c.source_id and
  p.content_object_id = rco.id AND
  p.user_id(+) = u.id  and
  u.id in ( select id
                  from users
                  where manager_id = $user.id$) 
                  AND
p.content_object_id NOT IN (41453457, 130020319, 43363877)
order by 
"Employee_Name", "Completion_Status"
+3
source share
5 answers

You can use something like:

ORDER BY "Employee_Name", CASE "Completed_Status"
                          WHEN 'Completed'  THEN 0
                          WHEN 'Passed'     THEN 1
                          WHEN 'Incomplete' THEN 2
                          WHEN 'Failed'     THEN 3
                          ELSE                   4
                          END;

You may need to change the CASE to work with the original value of "p.status", in which case the WHEN conditions will also change:

ORDER BY "Employee_Name", CASE p.status
                          WHEN 'C' THEN 0
                          WHEN 'P' THEN 1
                          WHEN 'I' THEN 2
                          WHEN 'F' THEN 3
                          ELSE          4
                          END;
+6
source

Edit: I now assume that your underlying data is the only char ...

order by decode(p.status,'P',1,'F',2,'C',3,'I',4) 
+3
source

:

SELECT ...
ORDER BY Employee_Name,
    (CASE WHEN Completion_Status = 'Completed' THEN 0
          WHEN Completion_Status = 'Passed' THEN 1
          WHEN Completion_Status = 'Incomplete' THEN 2
          WHEN Completion_Status = 'Failed' THEN 3
          ELSE 4
     END)
+3

SELECT:

decode(p.status,'P',2, 'F',4, 'C',1, 'I',3, 5) "Completion_Status Level",

ORDER BY?

+2

:

The following worked like a charm!

ORDER BY "Employee_Name", CASE "Completed_Status" 
                          WHEN 'Completed'  THEN 0 
                          WHEN 'Passed'     THEN 1 
                          WHEN 'Incomplete' THEN 2 
                          WHEN 'Failed'     THEN 3 
                          ELSE                   4 
                          END; 
0
source

Source: https://habr.com/ru/post/1765874/


All Articles