SQLServer2008快速递归查询
我有三张桌子t1、t2和t3。t1有我的第一点SQLServer2008快速递归查询,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有三张桌子t1、t2和t3。t1有我的第一点 -------------------------- | t1 -------------------------- | objectId, x, y <--(these are fields) -------------------------- | 30536, 1364690.09169,16518759.7879 | -------------------------- --------------------------
--------------------------
| t1
--------------------------
| objectId, x, y <--(these are fields)
--------------------------
| 30536, 1364690.09169,16518759.7879
|
--------------------------
--------------------------
|t1
--------------------------
|objectId,x,y 43667->43666->43665->43664->43370->11191)并到达我的最终目的地。因此,我所需要的只是结果中的起点(30536)和终点(11191)。这不是一个容易的结果,但我可以举个例子
这是一个具有与您类似的基本表结构和CTE
解决方案的示例。
基本上可以实现所需的递归查询CTE
。但这有点难,因为你有3张不同的桌子。相反,如果可以在一个表中定义所有点,并且可以为起点和终点添加一个不存在的值,则会更容易。(偶数为空)
表格结构:
CREATE TABLE startpoint(
id int,
x int,
y int
)
CREATE TABLE points(
id int,
fx int,
fy int,
tx int,
ty int
)
CREATE TABLE endpoint(
id int,
x int,
y int
)
INSERT INTO startpoint VALUES(1, 1,1)
INSERT INTO startpoint VALUES(6, 2,4)
INSERT INTO points VALUES (2, 1,1 , 2,1)
INSERT INTO points VALUES (3, 2,1 , 2,2)
INSERT INTO points VALUES (4, 2,4 , 2,5)
INSERT INTO points VALUES (7, 2,5 , 3,2)
INSERT INTO points VALUES (8, 3,2 , 3,3)
INSERT INTO points VALUES (9, 3,3 , 3,4)
INSERT INTO endpoint VALUES(5, 2,2)
INSERT INTO endpoint VALUES(10, 3,4)
WITH CTE_Points
AS
(
SELECT
-1 AS FromID,
s.ID AS ToID,
-1 AS fx,
-1 AS fy,
s.x as tx,
s.y as ty
FROM startpoint s
WHERE s.ID = 6
UNION ALL
SELECT
cte1.ToID AS FromID,
points.ID AS ToID,
points.fx,
points.fy,
points.tx,
points.ty
FROM points
INNER JOIN CTE_Points cte1 ON (points.fx = cte1.tx AND points.fy = cte1.ty)
OR (points.tx = cte1.fx AND points.ty = cte1.fy)
WHERE points.ID != cte1.ToID AND points.ID != cte1.FromID
UNION ALL
SELECT
e.ID AS FromID,
-1 AS ToID,
-1 AS fx,
-1 AS fy,
-1 AS tx,
-1 AS ty
FROM CTE_Points
INNER JOIN endpoint e ON (CTE_Points.fx = e.x AND CTE_Points.fy = e.y)
OR (CTE_Points.tx = e.x AND CTE_Points.ty = e.y)
OR (points.fx = cte1.fx AND points.fy = cte1.fy)
OR (points.tx = cte1.tx AND points.ty = cte1.ty)
WHERE e.ID != CTE_Points.ToID AND e.ID != CTE_Points.FromID
)
SELECT FromID AS ID FROM CTE_Points
WHERE FromID != -1
UNION
SELECT ToID AS ID FROM CTE_Points
WHERE ToID != -1
查询:
CREATE TABLE startpoint(
id int,
x int,
y int
)
CREATE TABLE points(
id int,
fx int,
fy int,
tx int,
ty int
)
CREATE TABLE endpoint(
id int,
x int,
y int
)
INSERT INTO startpoint VALUES(1, 1,1)
INSERT INTO startpoint VALUES(6, 2,4)
INSERT INTO points VALUES (2, 1,1 , 2,1)
INSERT INTO points VALUES (3, 2,1 , 2,2)
INSERT INTO points VALUES (4, 2,4 , 2,5)
INSERT INTO points VALUES (7, 2,5 , 3,2)
INSERT INTO points VALUES (8, 3,2 , 3,3)
INSERT INTO points VALUES (9, 3,3 , 3,4)
INSERT INTO endpoint VALUES(5, 2,2)
INSERT INTO endpoint VALUES(10, 3,4)
WITH CTE_Points
AS
(
SELECT
-1 AS FromID,
s.ID AS ToID,
-1 AS fx,
-1 AS fy,
s.x as tx,
s.y as ty
FROM startpoint s
WHERE s.ID = 6
UNION ALL
SELECT
cte1.ToID AS FromID,
points.ID AS ToID,
points.fx,
points.fy,
points.tx,
points.ty
FROM points
INNER JOIN CTE_Points cte1 ON (points.fx = cte1.tx AND points.fy = cte1.ty)
OR (points.tx = cte1.fx AND points.ty = cte1.fy)
WHERE points.ID != cte1.ToID AND points.ID != cte1.FromID
UNION ALL
SELECT
e.ID AS FromID,
-1 AS ToID,
-1 AS fx,
-1 AS fy,
-1 AS tx,
-1 AS ty
FROM CTE_Points
INNER JOIN endpoint e ON (CTE_Points.fx = e.x AND CTE_Points.fy = e.y)
OR (CTE_Points.tx = e.x AND CTE_Points.ty = e.y)
OR (points.fx = cte1.fx AND points.fy = cte1.fy)
OR (points.tx = cte1.tx AND points.ty = cte1.ty)
WHERE e.ID != CTE_Points.ToID AND e.ID != CTE_Points.FromID
)
SELECT FromID AS ID FROM CTE_Points
WHERE FromID != -1
UNION
SELECT ToID AS ID FROM CTE_Points
WHERE ToID != -1
您可以尝试将s.ID从6更改为1,如何分别选择两个“方式”
(注意:这仅在表中没有连接时有效,如:
Record1.FromX=Record2.FromX和Record1.FromY=Record2.FromY)我在t2上进行了多个(最多三个)自联接,不幸的是,有些多段线是from->fromex。端点端点我更新了我的答案,现在from->from也可以了。我也换了小提琴。