SQL案例顺序按特定顺序 [英] SQL Case Order By specific order

查看:105
本文介绍了SQL案例顺序按特定顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我之前也问过类似的问题,但是我已经对其进行了研究,但并没有明确发现这一点. 我有一个需要在OptionName(NVarChar)和IsActive(BIT)字段上排序的表.

Ok, I asked something similar before, but I've researched it and haven't found this specifically. I have a table that I need sorted on fields OptionName(NVarChar) and IsActive(BIT).

对于DDL,我需要结果按以下顺序排列:

I need the results to be in the following order for a DDL:

Option A
Option B
Option C
Options that are Active, by OptionName ASC 
Option D
Options that are Inactive, by OptionName ASC 

到目前为止,我已经拥有

So far I have

ORDER BY CASE WHEN PortalName = 'Company, Inc' THEN 0 ELSE 1 END,
         CASE WHEN PortalName = 'Setup' THEN 1 ELSE 2 END,
         CASE WHEN PortalName = 'Daily Routine' THEN 2 ELSE 3 END,
         CASE WHEN IsActive = 1 THEN 3 ELSE 4 END, 
         CASE WHEN PortalName = 'Master Option' THEN 4 ELSE 5 END,
         PortalName ASC

但这将结果返回为:

Option A
Option B
Option C
Option D
Options that are Active, by OptionName ASC 
Options that are Inactive, by OptionName ASC 

任何帮助都会很棒!

推荐答案

回答一个问题:当PortalName ='Master Option'则9999 ELSE 5 END

Got an answer: CASE WHEN PortalName = 'Master Option' THEN 9999 ELSE 5 END

这篇关于SQL案例顺序按特定顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆