2012-09-12 157 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)