2013-01-16 38 views
1

我有以下Location如何顯示在單獨的列位置結構

ID Name  ParentID 
----------------------- 
1 TopLevel NULL 
2 Region 1 1 
3 Fleet 1 2 
4 Fleet 2 2 

我最近剛剛完成了寫一個遞歸CTE在層次結構的方式來顯示這種結構:

TopLevel 
-Region 1 
--Fleet 1 
--Fleet 2 

我現在需要編寫一個查詢來以列分層格式返回此結構:

Level1  Level2 Level3 Level4 Level5 Level6 
Top Level 
Top Level Region 1 Fleet 1 
Top Level REgion 1 Fleet 2 

位置可以達到的最大深度是9.我將如何完成此操作?我試圖使用遞歸CTE並在此基礎上構建,但我認爲這不會起作用。任何其他想法?

+1

你應該在應用程序方面這樣做。 – Kermit

+0

沒有前端,全都在數據庫中。 – mameesh

+2

它是如何「在數據庫中的所有」?所有關心行與列的最終用戶都在Management Studio中查看結果? –

回答

1

這裏將是一種完成輸出的方法,使用遞歸CTE硬編碼來填充高達9層的層次結構(您需要根據需要將表/列引用更改爲實際對象名稱,並且您可能需要增加varchar(50)如果name列的實際類型比)寬:

with cte as (
    select id, 1 as level, 
    name as level1, 
    cast(null as varchar(50)) as level2, 
    cast(null as varchar(50)) as level3, 
    cast(null as varchar(50)) as level4, 
    cast(null as varchar(50)) as level5, 
    cast(null as varchar(50)) as level6, 
    cast(null as varchar(50)) as level7, 
    cast(null as varchar(50)) as level8, 
    cast(null as varchar(50)) as level9 
    from table1 t 
    where parentid is null 

    union all 

    select t.id, cte.level + 1 as level, 
    case when level = 0 then t.name else cte.level1 end as level1, 
    case when level = 1 then t.name else cte.level2 end as level2, 
    case when level = 2 then t.name else cte.level3 end as level3, 
    case when level = 3 then t.name else cte.level4 end as level4, 
    case when level = 4 then t.name else cte.level5 end as level5, 
    case when level = 5 then t.name else cte.level6 end as level6, 
    case when level = 6 then t.name else cte.level7 end as level7, 
    case when level = 7 then t.name else cte.level8 end as level8, 
    case when level = 8 then t.name else cte.level9 end as level9 
    from table1 t 
    inner join cte on cte.id = t.parentid 
) 
select * from cte; 

樣本輸出:

| ID | LEVEL | LEVEL1 | LEVEL2 | LEVEL3 | LEVEL4 | LEVEL5 | LEVEL6 | LEVEL7 | LEVEL8 | LEVEL9 | 
---------------------------------------------------------------------------------------------------- 
| 1 |  1 | TopLevel | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | 
| 2 |  2 | TopLevel | Region 1 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | 
| 3 |  3 | TopLevel | Region 1 | Fleet 1 | (null) | (null) | (null) | (null) | (null) | (null) | 
| 4 |  3 | TopLevel | Region 1 | Fleet 2 | (null) | (null) | (null) | (null) | (null) | (null) | 

演示:http://www.sqlfiddle.com/#!6/0bd5d/14

+0

這正是我正在尋找的。我所做的一切都完全相同,但在案件陳述中的ELSE是我所缺少的,謝謝! – mameesh

0

我可以看到使用9個UNION語句,所以你可能會得到你想要的輸出,但它不會很漂亮。

SELECT Name as Level1, '' as Level2, '' as Level3 
FROM Location 
WHERE ParentId IS NULL 
UNION 
SELECT L1.Name as Level1, L2.Name as Level2, '' as Level3 
FROM Location L1 
JOIN Location L2 
    ON L1.Id = L2.ParentId 
    AND L1.ParentId IS NULL 
UNION 
SELECT L1.Name as Level1, L2.Name as Level2, L3.Name as Level3 
FROM Location L1 
JOIN Location L2 
    ON L1.Id = L2.ParentId 
    AND L1.ParentId IS NULL 
JOIN Location L3 
    ON L2.Id = L3.ParentId 
    AND L1.ParentId IS NULL 

等等等等增加更多的表來加入,其後每級:

這裏有3個級別(你應該能夠相應地增加上)去。似乎可能有一個更好/更簡單的方法,只有我能想到的只有我的頭頂。

這是Fiddle

相關問題