Oracle查询是:
select t3.id,
t4.caption || '>' || t3.caption
from (
SELECT t1.id as id,
(t2.caption || '>' || t1.caption) as caption,
t2.pid as pid
FROM ParentChild T1, ParentChild T2
WHERE T1.pid = T2.id(+)
) t3,
(
SELECT t1.id as id,
(t2.caption || '>' || t1.caption) as caption,
t2.pid as pid
FROM ParentChild T1, ParentChild T2
WHERE T1.pid = T2.id(+)
) t4
where t3.pid = t4.id(+);
输出:
ID T4.CAPTION||'>'||T3.CAPTION
---------------------- ---------------------------
4 a>b>c>d
3 >a>b>c
1 >>a
2 >a>b
5 >a>e
我需要等效的SQL Server查询...
答案 0 :(得分:2)
尝试以下查询;)
SELECT t3.id, t4.caption + '>' + t3.caption
FROM (
SELECT t1.id as id, (t2.caption + '>' + t1.caption) as caption, t2.pid as pid
FROM ParentChild T1
LEFT JOIN ParentChild T2
ON T1.pid = T2.id) t3
LEFT JOIN (
SELECT t1.id as id, (t2.caption + '>' + t1.caption) as caption, t2.pid as pid
FROM ParentChild T1
LEFT JOIN ParentChild T2
ON T1.pid = T2.id) t4 ON t3.pid = t4.id;