本文介绍了层次结构(SQL SERVER)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个层次表,如下所示:

I have a hierarchy table which looks like this:

RowID   Code       Name             ParentID   Level
1        GC         Gulf Coast        NULL       1
21       GC.1       Texas             1          2
28       GC.1.1     Houston           2          3
34       GC.1.2     Galveston         2          3
67       GC.1.1.1   Hou Ship Channel  3          4 



想知道我如何查询这些数据,以便结果数据显示如下:


Would like to know how I can query this data so that the resulting data would appear as such:

RowID   Level1 Name       Level2 Name      Level3 Name      Level4 Name
1        Gulf Coast        NULL             NULL
21       Gulf Coast        Texas            NULL
28       Gull Coast        Texas            Houston
34       Gull Coast        Texas            Galveston
67       Gull Coast        Texas            Houston          Hou Ship Channel





我写的查询只返回最后一级。你能帮忙吗。



谢谢Jake



The query I've written only is only returning the last level. Can you please help.

Thanks Jake

推荐答案



这篇关于层次结构(SQL SERVER)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 02:59