SQL - 指定的字母顺序

时间:2010-09-21 19:04:41

标签: sql oracle plsql oracle9i

我想要一个Completion_Status列的特定订单,如:

已完成,已通过,未完成且已完成失败

我该如何订购?我想定义上面的顺序。

我试过了CASE,但它给了我一个错误,就像期待NUM不是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"

5 个答案:

答案 0 :(得分:6)

您可以使用以下内容:

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;

您可能需要更改CASE以处理原始'p.status'值,在这种情况下WHEN条件也会改变:

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;

答案 1 :(得分:3)

编辑:我现在假设您的基础数据是单个字符...

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

答案 2 :(得分:3)

您必须自己在案例陈述中提供订单:

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 :(得分:2)

您可以向SELECT添加新行:

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

然后订购它?

答案 4 :(得分:0)

谢谢大家的回复:

以下工作就像一个魅力!

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;