如何按文档章节排序

How to order by document chapter

我需要创建存储过程以从 table 获取所有文档标题,但它必须以首先显示文档章节的方式排序。我真的不知道自己该怎么做。

必须这样排序:

ID    Chapter      Description
1     0001-0299    Title
2     0001-0019    Title
3     0001         "some text"
4     0002         "some text"
.
.
.     0021-0039    Title
.     0021         "some text"
.     0022         "some text"

我想你明白了。有人可以提供帮助吗?感谢您的帮助。

这将按 ID 排序,然后按章节排序。

IF OBJECT_ID ( 'orderedDocuments', 'P' ) IS NOT NULL 
    DROP PROCEDURE orderedDocuments;
GO
CREATE PROCEDURE orderedDocuments
AS   
    SELECT
        ID, Chapter, Description
    FROM
        [Table Name]
    ORDER BY
         ID DESC
         ,Chapter DESC
GO

也许你打算这样做:

select id, chapter, description
from t
order by left(chapter, 4),
         (case when chapter like '%-%' then 1 else 2 end),
         chapter;

这按章节的前四个字符排序,然后按带连字符的章节排序,最后按章节本身排序。如果您需要前两个值按 "other" 顺序排列,则:

select id, chapter, description
from t
order by left(chapter, 4),
         (case when chapter like '%-%' then 1 else 2 end),
         (case when chapter like '%-%' then chapter end) desc,
         chapter asc;