与层次结构顺序和缩进空间相关的查询

时间:2016-08-05 05:20:33

标签: sql sql-server database

输入表:区域

    +---------------+---------------+---------- +-----------+
    | Child         | Parent        | Level     |  levelname|
    +---------------+---------------+---------- +-----------+
    | All Region    | All Region    | 1         |  national |
    | Africa Region | All Region    | 2         |   region  |
    | America       | All Region    | 2         |   region  |
    | Asia          | All Region    | 2         |   region  |
    | Europe Region | All Region    | 2         |   region  |
    | Africa        | Africa Region | 3         | Subregion |
    | Asia Pacific  | Asia          | 3         | Subregion |
    | Europe        | Europe Region | 3         | Subregion |
    | North America | America       | 3         | Subregion |
    | South America | America       | 3         | Subregion | 
    | Argentina     | South America | 4         |   Country |
    | Australia     | Asia Pacific  | 4         |   Country |
    | Pakistan      | Asia Pacific  | 4         |   Country | 
    | South Africa  | Africa        | 4         |   Country |
    | Tunisia       | Africa        | 4         |   Country | 
    | Uruguay       | South America | 4         |   Country |
    +-------------------------------------------------------+

此处,区域为4级

  • 所有地区
  • 区域
  • 子区域
  • 国家

。输出应如下:

National
   Region_1
     SubRegion_1_1
       Country_1_1_1
       Country_1_1_2
    SubRegion_1_2
       Country_1_2_1
       Country_1_2_2
       Country_1_2_3
       Country_1_2_4
   Region_2
            …. And so on.

输出有一些缩进的空格来显示视觉效果,我不知道如何开始查询。

3 个答案:

答案 0 :(得分:1)

你需要recursive cte来获取层次结构中的数据,

-- populate test data
DECLARE @tbl TABLE (Child VARCHAR(100), Parent VARCHAR(100), Level INT, LevelName VARCHAR(100))

INSERT INTO @tbl
VALUES
    ('AllRegion'        ,'AllRegion'        ,1      ,'national')
    ,('AfricaRegion'    ,'AllRegion'        ,2      ,'region')
    ,('America'         ,'AllRegion'        ,2      ,'region')
    ,('Asia'            ,'AllRegion'        ,2      ,'region')
    ,('EuropeRegion'    ,'AllRegion'        ,2      ,'region')
    ,('Africa'          ,'AfricaRegion'     ,3      ,'Subregion')
    ,('AsiaPacific'     ,'Asia'             ,3      ,'Subregion')
    ,('Europe'          ,'EuropeRegion'     ,3      ,'Subregion')
    ,('NorthAmerica'    ,'America'          ,3      ,'Subregion')
    ,('SouthAmerica'    ,'America'          ,3      ,'Subregion')
    ,('Argentina'       ,'SouthAmerica'     ,4      ,'Country')
    ,('Australia'       ,'AsiaPacific'      ,4      ,'Country')
    ,('Pakistan'        ,'AsiaPacific'      ,4      ,'Country')
    ,('SouthAfrica'     ,'Africa'           ,4      ,'Country')
    ,('Tunisia'         ,'Africa'           ,4      ,'Country')
    ,('Uruguay'         ,'SouthAmerica'     ,4      ,'Country')


-- use this query
;WITH Cte AS
(
    SELECT *
        ,ROW_NUMBER() OVER (PARTITION BY t.LevelName, t.Parent ORDER BY t.Level) AS RowNo   -- assing a number to use for sorting 
    from @tbl t
),
Final AS
(
    SELECT t.child, t.Parent, t.Level
        ,CAST(RowNo AS VARCHAR(MAX)) AS SortBy      -- this will be used for sorting.
    FROM Cte t
    WHERE t.level = 1

    UNION ALL 

    SELECT c.child, c.parent, c.level
        ,CAST(p.SortBy + CAST(c.RowNo AS VARCHAR(MAX))AS VARCHAR(MAX))  AS SortBy       -- keep adding the levels to the sort order
    FROM Cte c
    INNER JOIN Final p On c.Parent = p.child 
        AND c.Level > 1 -- this is required as the top parent is not set as NULL
)
SELECT Child
    ,SortBy
    ,SPACE(Level * 4 ) + Child AS HierarcyText
FROM Final
ORDER BY SortBy
OPTION(MAXRECURSION 0)

希望这会对你有所帮助。

结果:

Child             SortBy    HierarcyText               
----------------- ------- -----------------------------
AllRegion         1           AllRegion
AfricaRegion      11              AfricaRegion
Africa            111                 Africa
SouthAfrica       1111                    SouthAfrica
Tunisia           1112                    Tunisia
America           12              America
NorthAmerica      121                 NorthAmerica
SouthAmerica      122                 SouthAmerica
Uruguay           1221                    Uruguay
Argentina         1222                    Argentina
Asia              13              Asia
AsiaPacific       131                 AsiaPacific
Australia         1311                    Australia
Pakistan          1312                    Pakistan
EuropeRegion      14              EuropeRegion
Europe            141                 Europe

答案 1 :(得分:0)

$( "#a" ).load( "article.html" );
  1. 将以下结果保存到临时表
  2. 添加到所有非空值选项卡(CHAR(9)),CRLF(CHAR(13)+ CHAR(10))
  3. 将所有内容合并为一个字符串
  4. 打印字符串
  5. 我只完成了步骤(1),步骤(2) - (4)很容易。如果还不清楚,请告诉我。

答案 2 :(得分:0)

SQL recursive CTE query可以帮助在SQL Server中定义层次结构

以下是在SQL中使用递归查询的方法

;with cte as (
    select 
        Child, Parent, Level, LevelName, 
        rn = CONVERT(nvarchar(max), ROW_NUMBER() Over (Partition By Level Order By Child) )
    from Regions where Level = 1
    union all
    select
        r.Child, r.Parent, r.Level, r.LevelName, 
        rn = rn + '_' + CONVERT(nvarchar(max), ROW_NUMBER() Over (Partition By r.Level Order By r.Child) )
    from Regions r
    inner join cte 
        on  r.Level = (cte.Level + 1) and
            r.Parent = cte.Child
)
select * 
from cte
order by rn

enter image description here