帮助递归 CTE 查询连接到第二个表
我的目标是递归遍历表 tbl,并在遍历该表时从另一个表 tbl2 中选择一个国家/地区缩写(如果存在),并将这些结果附加在一起,这些结果包含在最终输出中。
我将使用的示例将来自这篇文章
tbl2 有一个到 tbl 的外键“tbl_id”,看起来像这样
INSERT INTO @tbl2( Id, Abbreviation, tbl_id )
VALUES
(100, 'EU', 1)
,(101, 'AS', 2)
,(102, 'DE', 3)
,(103, 'CN', 5)
*注意:并非所有国家/地区都有缩写。
诀窍是,我希望亚洲的所有国家至少显示亚洲的缩写,即“AS”,即使一个国家没有缩写(例如印度)。如果该国家/地区确实有缩写结果需要如下所示:China:CN,AS
我已经使用子查询部分地工作了,但印度总是为缩写返回NULL。它的行为就像如果没有完整的递归路径返回缩写,那么它返回 null。也许解决方案是在缩写表上使用左外连接?我已经尝试了几个小时许多不同的变体,并且子查询是我能得到的最接近的。
WITH abcd
AS (
-- anchor
SELECT id, [Name], ParentID,
CAST(([Name]) AS VARCHAR(1000)) AS "Path"
FROM @tbl
WHERE ParentId IS NULL
UNION ALL
--recursive member
SELECT t.id, t.[Name], t.ParentID,
CAST((a.path + '/' + t.Name + ':' +
(
select t2.abbreviation + ','
from @tbl2
where t.id = t2.id
)) AS VARCHAR(1000)) AS "Path"
FROM @tbl AS t
JOIN abcd AS a
ON t.ParentId = a.id
)
SELECT * FROM abcd
顺便说一句,如果这很重要的话,我正在使用 sql server 2005
My objective is to recurse through table tbl and while recursing through that table select a country abbreviation (if it exists) from another table tbl2 and append those results together which are included in the final output.
The example I'll use will come from this post
tbl2 has a Foreign Key 'tbl_id' to tbl and looks like this
INSERT INTO @tbl2( Id, Abbreviation, tbl_id )
VALUES
(100, 'EU', 1)
,(101, 'AS', 2)
,(102, 'DE', 3)
,(103, 'CN', 5)
*Note: not all the countries have abbreviations.
The trick is, I want all the countries in Asia to at least show the abbreviation of Asia which is 'AS' even if a country doesn't have an abbreviation (like India for example). If the country does have an abbreviation the result needs to look like this: China:CN,AS
I've got it partly working using a subquery, but India always returns NULL for the abbreviation. It's acting like if there isn't a full recursive path back to the abbreviation, then it returns null. Maybe the solution is to use a left outer join on the abbreviation table? I've tried for hours many different variations and the subquery is as close as I can get.
WITH abcd
AS (
-- anchor
SELECT id, [Name], ParentID,
CAST(([Name]) AS VARCHAR(1000)) AS "Path"
FROM @tbl
WHERE ParentId IS NULL
UNION ALL
--recursive member
SELECT t.id, t.[Name], t.ParentID,
CAST((a.path + '/' + t.Name + ':' +
(
select t2.abbreviation + ','
from @tbl2
where t.id = t2.id
)) AS VARCHAR(1000)) AS "Path"
FROM @tbl AS t
JOIN abcd AS a
ON t.ParentId = a.id
)
SELECT * FROM abcd
btw, I'm using sql server 2005 if that matters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个例子,它会给你输出(1个样本行)
TSQL是
Try this example, which will give you the output (1 sample row)
The TSQL being