2012-09-12 112 views
2

我有三张表t1,t2和t3。 T1有我的第一点Sql Server 2008快递递归查询

-------------------------- 
| t1 
-------------------------- 
| objectId, x, y  <--(these are fields) 
-------------------------- 
| 30536, 1364690.09169,16518759.7879 
| 
-------------------------- 

T2有我的几个折线这些都是端点他们

-------------------------- 
| t2 
-------------------------- 
| objectId, from_x, from_y, to_x, to_y  <--(these are fields) 
-------------------------- 
| 43664, 1364815.8770, 16518764.8200, 1364806.6780, 16518760.9000 
| 43665, 1364806.6780, 16518760.9000, 1364710.2130, 16518719.7700 
| 43666, 1364710.2130, 16518719.7700, 1364709.4300, 16518720.3000 
| 43667, 1364709.4300, 16518720.3000, 1364690.0920, 16518759.7900 
| 43370, 1364843.6870, 16518667.7600, 1364815.8770, 16518764.8200 
|------------------------- 

T3有我的整条生产线的我的最后终点

-------------------------- 
| t3 
-------------------------- 
| objectId, x, y  <--(these are fields) 
-------------------------- 
| 11191, 1364843.68657, 16518667.7589 
| 
-------------------------- 

我做舍入到小数点后两位,以便终点在某点或另一点匹配。 我需要做的是创建一些类型的递归查询来完成从开始到所有连接多段线到最终端点的行。现在一些折线并不总是从 - >到它可能是另一种方式 - >从像组合这样的类型开始。在这个例子中

线(30536 - > 43667 - > 43666 - > 43665 - > 43664 - > 43370 - > 11191),并达到我的最终目的地。所以我需要的是我的结果中的起点(30536)和终点(11191)。

+0

我做了多个(最多3个)自我加入t2 – Fonzy

+0

我试图帮助一个CTE –

+0

不幸的是,有一些多段线是从 - >从 – Fonzy

回答

2

不是一件容易的事,但我可以举个例子。

这是一个SQLFiddle,它有一个基本的表结构,如您的和CTE解决方案。 基本完成您需要的递归查询CTE。但是由于你有3个不同的表格,所以它有点难度。相反,如果您可以在一个表格中定义所有点,并且可以添加一个不存在的值,则开始和结束点将更容易。 (甚至是NULL)。

表结构:

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,它是如何选择这两个 “办法” separatly。

注:这只有当你在你的表格一样,没有连接的工作原理: Record1.FromX = Record2.FromX AND Record1.FromY = Record2.FromY)