有没有办法在ORDER BY之上的某些结果的SQL ORDER BY列ASCENDING?

时间:2017-11-07 16:57:31

标签: sql sql-server tsql

我有一个100行的查找表,我用它来生成网页上的垂直复选框列表。非常简单的查询来获得结果:

SELECT servicetype FROM XXX

我想知道是否有一种方法可以对列进行ORDER BY,但是在排序列表的顶部是否有某些已知值?有点像(我甚至不知道怎么写这个伪代码但是......)

SELECT servicetype FROM XXX
ORDER BY servicetype ASC 
WITH servicetype IN ('Personal', 'Juggling') ON TOP

所以我的列表看起来像是:

Juggling         - On top regardless of ORDER BY
Personal         - On top regardless of ORDER BY
Anteating        - Everything else is ORDER BY
Barflying
Beafeating
Carswatting
etc...

感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

您可以使用:

SELECT servicetype FROM XXX
ORDER BY 
    CASE WHEN servicetype IN ('Personal', 'Juggling') THEN 0 ELSE 1 END ASC
  , servicetype ASC 

答案 1 :(得分:4)

SELECT servicetype 
FROM XXX
order by case when servicetype = 'Juggling' then 'a'
              when servicetype = 'Personal' then 'b'
              else servicetype
         end