2010-09-21 76 views
3

我想爲Completion_Status列特定的順序爲:SQL - 指定的字母順序

完成,傳遞不完整&失敗

我怎麼做我訂購?我想要定義上面的順序。

我試過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" 
+0

您使用了哪個數據庫?有些人比其他人有更好的設施,所以它有助於知道。 – Oded 2010-09-21 19:05:39

+0

這是一個oracle 9i數據庫。 – 2010-09-21 19:07:27

回答

3

編輯:我現在假設你的基礎數據是單個字符...

order by decode(p.status,'P',1,'F',2,'C',3,'I',4) 
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

+1:9i是第一個支持CASE的版本。在9i之前,它必須是'DECODE' ... – 2010-09-21 19:15:10

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) 
2

你可以添加一個新的行來選擇:

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

,然後ORDER BY呢?

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; 
+1

它很有效。也許你可以接受其中的一個答案? – 2010-09-22 16:52:44