如何按案例顺序应用降序?

How to apply descending order in order by case?

我有一个简单的 table,我想按特定规则对其进行排序。我想要的输出如下所示:

 PERSON_THIRD_BUF
 PERSON_TWO_BUF
 PERSON_TWO_BUF_11
 PERSON_TWO_BUF_10
 PERSON_TWO_BUF_9
 PERSON_TWO_BUF_8
 PERSON_TWO_BUF_7
 PERSON_TWO_BUF_6
 PERSON_TWO_BUF_5
 PERSON_TWO_BUF_4
 PERSON_TWO_BUF_3
 PERSON_TWO_BUF_2
 PERSON_TWO_BUF_1

所以PERSON_THIRD_BUF应该是第一个,PERSON_TWO_BUF应该是第二个,PERSON_TWO_BUF_*应该是降序:

我已经设法对 PERSON_TWO_BUFPERSON_THIRD_BUF 进行排序,但是我不知道如何按降序对 PERSON_TWO_BUF_* 进行排序。

DECLARE @t table(col1 VARCHAR(50));


insert into @t 
values
( N'PERSON_TWO_BUF' ), 
( N'PERSON_THIRD_BUF' ), 
( N'PERSON_TWO_BUF_1' ), 
( N'PERSON_TWO_BUF_2' ), 
( N'PERSON_TWO_BUF_3' ), 
( N'PERSON_TWO_BUF_4' ), 
( N'PERSON_TWO_BUF_5' ), 
( N'PERSON_TWO_BUF_6' ), 
( N'PERSON_TWO_BUF_7' ), 
( N'PERSON_TWO_BUF_8' ), 
( N'PERSON_TWO_BUF_9' ), 
( N'PERSON_TWO_BUF_10' ), 
( N'PERSON_TWO_BUF_11' )

SELECT * FROM @t
ORDER BY CASE
      WHEN COL1 = 'PERSON_THIRD_BUF' THEN 0
    WHEN COL1 = 'PERSON_TWO_BUF' THEN 1
    WHEN COL1 LIKE 'PERSON_TWO_BUF_%' THEN 3
    END

任何帮助将不胜感激

一个可能的选项是以下 ORDER BY 子句(基于问题的尝试):

SELECT * FROM @t
ORDER BY 
   CASE
      WHEN COL1 = 'PERSON_THIRD_BUF' THEN 0
      WHEN COL1 = 'PERSON_TWO_BUF' THEN 1
      WHEN COL1 LIKE 'PERSON_TWO_BUF_%' THEN 2
      ELSE 3
   END,
   TRY_CONVERT(int, REPLACE(col1, 'PERSON_TWO_BUF_', '')) DESC