c*****t 发帖数: 1879 | 1 我有一 DAG table T,里面是
src name,
dst name,
value int
比如
a, b, 1
b, c, 2
a, d, 3
我现在需要做很多 path 的 query,需要得到 int[],也就是说
select getPath ('a', 'c');
结果应该是
[1,2]
有可能有多种 path,可以取最短的。
我之所以需要做该 query,是我有另外一个 table inputTable 是 pair,
需要得到。注意的是该 table 里有很多重复的 src, dst 。
select compute (getPath (T.src, T.dst), T.cost)
from inputTable T;
有什么好的办法?
多谢 | n********6 发帖数: 1511 | 2 没看懂啊。
getPath(a,c)怎么就从
里面得到(1,2)呢? | j*****n 发帖数: 1781 | 3 this is the classic parent/child relationship, in SQL Server, the levers are
limited, can't remember how deep it can be, but it is not worth a try...
select lvl1.value, lvl2.value, lvl3.value, ...,lv(n-1).value, lvn.value
from
(select src, dst from T) as lvl1
left join
(select src, dst from T) as lvl2
on lvl1.dst = lvl2.src
left join
(select src, dst from T) as lvl3
on lvl2.dst = lvl3.src
...
left join
(select src, dst from T) as lvln
on lvl(n-1).dst = lvln.src
where lvl1.src = 'a' |
|