层次结构(SQL SERVER) [英] Hierarchy (SQL SERVER)

查看:105
本文介绍了层次结构(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) [ ^ ]。



在这里CP你'我会找到很多关于获取分层数据的文章。在本网站的右上角使用 SearchBox
I'd suggest to read this: Hierarchical Data (SQL Server)[^].

Here on CP you'll find a lot of articles about fetching hierarchical data. Use SearchBox on the right-top corner of this site.


我发现使用UNION让我想要我需要。谢谢所有回复。
I figured out that using a UNION gets me want I need. Thank you all that replied.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆