SQL - 指定字母顺序

发布于 2024-09-24 19:49:49 字数 995 浏览 3 评论 0原文

我想要 Completion_Status 列的特定顺序,如下所示:

已完成、已通过、未完成和已通过。失败

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

我尝试了 CASE,但它给了我一个错误,如 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"

I want a specific order for the Completion_Status column as in:

Completed, Passed, Incomplete & Failed

How do I do I order this? I want to define the order above.

I tried CASE but it give 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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

九公里浅绿 2024-10-01 19:49:49

您也许可以使用类似的内容:

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;

You may be able to 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 might need to change the CASE to work on the raw 'p.status' value, in which case the WHEN conditions change too:

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;
心如荒岛 2024-10-01 19:49:49

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

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

Edit: i now assume your base data is the single char...

order by decode(p.status,'P',1,'F',2,'C',3,'I',4) 
嘿看小鸭子会跑 2024-10-01 19:49:49

您必须在案件陈述中自行提供订单:

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)

You have to provide the order yourself in your case statement:

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)
梦情居士 2024-10-01 19:49:49

您可以在 SELECT: 中添加一个新行,

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

然后对其进行 ORDER BY 吗?

Can you add a new line to SELECT:

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

and then ORDER BY it?

陌路黄昏 2024-10-01 19:49:49

谢谢大家的回复:

以下内容非常有效!

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; 

Thank you all for your replies:

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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文