从自连接表中获取父级的父级的父级
请复制并运行以下脚本。
DECLARE @Locations TABLE
(
LocationId INT,
LocationName VARCHAR(50),
ParentId INT
)
INSERT INTO @Locations SELECT 1, 'Europe', NULL
INSERT INTO @Locations SELECT 2, 'UK', 1
INSERT INTO @Locations SELECT 3, 'England', 2
INSERT INTO @Locations SELECT 4, 'Scotland', 2
INSERT INTO @Locations SELECT 5, 'Wales', 2
INSERT INTO @Locations SELECT 6, 'Cambridgeshire', 3
INSERT INTO @Locations SELECT 7, 'Cambridge', 6
INSERT INTO @Locations SELECT 8, 'North Scotland', 4
INSERT INTO @Locations SELECT 9, 'Inverness', 8
INSERT INTO @Locations SELECT 10, 'Somerset', 3
INSERT INTO @Locations SELECT 11, 'Bath', 10
INSERT INTO @Locations SELECT 12, 'Poland', 1
INSERT INTO @Locations SELECT 13, 'Warsaw', 12
我需要以下结果
谢谢。
Pleae copy and run following script.
DECLARE @Locations TABLE
(
LocationId INT,
LocationName VARCHAR(50),
ParentId INT
)
INSERT INTO @Locations SELECT 1, 'Europe', NULL
INSERT INTO @Locations SELECT 2, 'UK', 1
INSERT INTO @Locations SELECT 3, 'England', 2
INSERT INTO @Locations SELECT 4, 'Scotland', 2
INSERT INTO @Locations SELECT 5, 'Wales', 2
INSERT INTO @Locations SELECT 6, 'Cambridgeshire', 3
INSERT INTO @Locations SELECT 7, 'Cambridge', 6
INSERT INTO @Locations SELECT 8, 'North Scotland', 4
INSERT INTO @Locations SELECT 9, 'Inverness', 8
INSERT INTO @Locations SELECT 10, 'Somerset', 3
INSERT INTO @Locations SELECT 11, 'Bath', 10
INSERT INTO @Locations SELECT 12, 'Poland', 1
INSERT INTO @Locations SELECT 13, 'Warsaw', 12
I need following kind of result
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于当前的数据集,您无法做到这一点;您如何知道在
LocationId=11
的情况下,您有一个县/国家/大陆,而在LocationId=13
的情况下,没有县 - 只是一个国家/大陆??您如何知道从输出结果中“跳过”萨默塞特、北苏格兰等条目?
您在这里肯定需要更多信息......
通过此递归 CTE(通用表表达式)查询,您可以将“梯子”从层次结构上升到顶部,对于任何给定位置:
此 CTE 有效在 SQL Server 2005 及更高版本上 - 在 SQL Server 2000 上,不幸的是(是时候升级了!!)。
这再次允许您在单个条目的层次结构中向上移动 - 但它不可能返回您正在查找的数据集 - 没有足够的信息来根据当前数据确定这一点。
对于
@LocID=13
(华沙),您将得到以下输出:对于
@LocID=7
(剑桥),您将得到:从那里开始,您必须在您的应用程序中使用一些智能功能来获得您正在寻找的确切输出。
There's no way you can do this with the current set of data; how would you know that in the case of
LocationId=11
, you have a county/country/continent, while in the case ofLocationId=13
, there's no county - just a country/continent??And how do you know to "skip" the entries for
Somerset
,North Scotland
etc. from your output result??You definitely need more information here....
With this recursive CTE (Common Table Expression) query, you can get the "ladder" up the hierarchy to the top, for any given location:
This CTE works on SQL Server 2005 and up - on SQL Server 2000, you're out of luck, unfortunately (time to upgrade!!).
This again allows you to walk up the hierarchy for a single entry - but it cannot possibly return that data set you're looking for - there's not enough information to determine this from the current data.
For
@LocID=13
(Warsaw), you get this output:and for
@LocID=7
(Cambridge), you get:From there on, you'd have to use some smarts in your app to get the exact output you're looking for.