基于父子关系在SQL Server中对数据进行排序

时间:2016-02-04 06:02:05

标签: sql sql-server sql-server-2008

我有一个表格,我存储有关章节的详细信息,我必须按照索引表的顺序显示数据

  
      
  1. 第一章

         

    1.1第一章第1页

         

    1.2第一章第2页

  2.   
  3. 第二章

         

    2.1第二章第1页

         

    2.2第二章第2页

  4.   
  5. 第三章

         

    标题一

         

    标题二

         

    标题三

         

    3.1第三章第1页

         

    3.2第三章第2页

         

    3.3第三章第3页

  6.   

我们可以按排序或未排序的顺序在数据库中插入数据。但是数据应该基于父页面和子页面的pageOrder按排序顺序显示

我已经设置了SQL Fiddle但由于某种原因我无法保存SQL。您将在下面找到小提琴链接和详细信息

CREATE TABLE [Book]
(
     [id] int, 
     [Chapter] varchar(20), 
     [PageOrder] int, 
     [parentID] int
);

INSERT INTO [Book] ([id], [Chapter], [PageOrder],  [parentID])
VALUES
    ('1', 'Chapter One', 1, 0),
    ('2', 'Chapter Two', 2, 0),
    ('3', 'Chapter Three', 3, 0),
    ('4', 'Chapter Four', 4, 0),
    ('5', 'Chapter Five', 5, 0),
    ('6', 'Chapter One Page 1', 1, 1),
    ('7', 'Chapter One Page 2', 2, 1),
    ('8', 'Chapter One Page 3', 3, 1),
    ('9', 'Chapter One Page 4', 4, 1),
    ('10', 'Chapter Two Page 1', 1, 2),
    ('11', 'Chapter Two Page 3', 3, 2),
    ('12', 'Chapter Two Page 2', 2, 2),
    ('13', 'Chapter Three Tite 1', 0, 3),
    ('14', 'Chapter Three Tite 2', 0, 3),
    ('15', 'Chapter Three Tite 3', 0, 3),
    ('16', 'Chapter Three Page 2', 2, 3),
    ('17', 'Chapter Three Page 3', 3, 3),
    ('18', 'Chapter Three Page 1', 1, 3);

WITH CTE(ID, parentID, Chapter, PageOrder, Depth, SortCol) AS  
( 
    SELECT 
        ID, parentID, Chapter, PageOrder, 0, 
        CAST(ID AS varbinary(max)) 
    FROM Book 
    WHERE parentID = 0 

    UNION ALL

    SELECT 
        d.ID, d.parentID, d.Chapter, d.PageOrder, p.Depth + 1, 
        CAST(SortCol + CAST(d.ID AS binary(4)) AS varbinary(max)) 
    FROM Book AS d 
    JOIN CTE AS p ON d.parentID = p.ID 
) 
SELECT 
    ID, parentID, Chapter, PageOrder, Depth, 
    REPLICATE('--', Depth) + Chapter as PageName 
FROM CTE 
ORDER BY SortCol

此CTE查询正在对数据进行排序,但子页面没有正确排序的子页面按排序顺序显示,其中它们保存在数据库中

SqlFiddle链接http://www.sqlfiddle.com/#!3/9770a/1

3 个答案:

答案 0 :(得分:2)

完整的解决方案,如

SELECT * FROM
    (
     SELECT p.CategoryID
       , p.Category_Name
       , p.IsParent
       , p.ParentID
       , p.Active
       , p.Sort_Order AS Primary_Sort_Order
       , CASE WHEN p.IsParent = 0 THEN (SELECT Sort_Order FROM tbl_Category WHERE 
     CategoryID = p.ParentID) ELSE p.Sort_Order END AS Secondary_Sort_Order
      FROM tbl_Category p
    ) x
    ORDER BY Secondary_Sort_Order,
            CASE WHEN ParentID = 0 THEN CategoryID ELSE ParentID END,
CASE WHEN ParentID = 0 THEN 0 ELSE Primary_Sort_Order END, Primary_Sort_Order ASC

希望有帮助...!

答案 1 :(得分:1)

使用PageOrder代替ID来构建SortCol,同时使用VARCHAR(MAX)代替VARBINARY(MAX)

WITH CTE(ID, parentID, Chapter, PageOrder, Depth, SortCol)  AS  ( 
  SELECT ID, parentID, Chapter,PageOrder, 0, 
         CAST(PageOrder AS varchar(max)) 
  FROM Book 
  WHERE parentID = 0 

  UNION ALL

  SELECT d.ID, d.parentID, d.Chapter, d.PageOrder, p.Depth + 1, 
  CAST(SortCol + CAST(d.PageOrder AS varchar(max)) AS varchar(max)) 
  FROM Book AS d 
  JOIN CTE AS p ON d.parentID = p.ID 
)   
SELECT ID, parentID, Chapter, PageOrder, Depth, SortCol,
       REPLICATE('--', Depth) + Chapter as PageName 
FROM CTE 
ORDER BY SortCol, Chapter

此外,Chapter列用于对具有相同PageOrder且属于同一树级别的章节进行排序。

Demo here

答案 2 :(得分:0)

SELECT *, REPLICATE('--', CASE WHEN parentID > 0 THEN 1 ELSE 0 END) + Chapter AS PageName 
FROM Book 
ORDER BY COALESCE(NULLIF(parentID,0), id), parentID, PageOrder