Запрос, вычисляющий минимальное расстояние между узлами ациклического ориентированного графа
WITH TC(src_pid, tgt_pid, distance)
AS
( SELECT src_pid, tgt_pid, 1 FROM Links

  UNION ALL

  SELECT P.src_pid, C.tgt_pid, P.distance+1
  FROM Links AS C
    JOIN TC AS P
      ON C.src_pid = P.tgt_pid )
SELECT src_pid, tgt_pid, MIN(distance) AS min_distance
FROM TC
GROUP BY src_pid, tgt_pid
ORDER BY src_pid, tgt_pid;