0

我是 MS SQL-Server 的新手。我对此有疑问。
我有一个查询:

with by_segment as (
SELECT  
    Road.Road_ID,
    GEOMETRY::STGeomFromText(Track.Track.STAsText(),4326) as the_geom,
    row_number() over (partition by road_id order by Segment_Id) as [rn],
    count(*) over (partition by road_id) as [c]
FROM dbo.Road
LEFT JOIN Segment_ID  ON Road.Road_ID = Segment_ID.Road_ID
LEFT JOIN Track ON Segment_ID.Segment_ID = Track.Segment_ID
),
roads_by_segment as (
select 
   road_id,
   the_geom,
   [rn],
   [c]
from by_segment
where [rn] = 1

union all

select 
   [a].road_id,
   [a].the_geom.STUnion([b].the_geom),
   [b].[rn],
   [b].[c]
from by_segment as [a]
inner join roads_by_segment as [b]
   on [a].segment_id = b.[segment_id]
   and [a].[rn] = [b].[rn]+1
)

select * from roads_by_segment where [rn] = [c]

但是会出错(我尝试从俄语翻译):

Message 209, level 16, state 1, line 5
Ambiguous column name "road_id".
Message 209, level 16, state 1, line 5
Ambiguous column name "Segment_Id".
Message209, level 16, state 1, line 6
Ambiguous column name "road_id".

第 5 行和第 6 行:

    row_number() over (partition by road_id order by Segment_Id) as [rn],
    count(*) over (partition by road_id) as [c]

这里有什么问题?

更新

我从bluefeet答案中做所有事情并得到新的错误:

on [a].segment_id = b.[segment_id]

它说:

Message 207, level 16, state 1, line 29
Ambiguous column name "segment_id".
Message 207, level 16, state 1, line 29
Ambiguous column name "segment_id".
4

2 回答 2

3

您必须以road_id它来自的表格开头。每当您在多个表中具有相同名称的列时,您需要在列前加上表名:

with by_segment as (
SELECT  
    Road.Road_ID,
    GEOMETRY::STGeomFromText(Track.Track.STAsText(),4326) as the_geom,
    row_number() over (partition by Road.road_id order by Segment_ID.Segment_Id) as [rn],
    count(*) over (partition by Road.road_id) as [c]
FROM dbo.Road
LEFT JOIN Segment_ID  ON Road.Road_ID = Segment_ID.Road_ID
LEFT JOIN Track ON Segment_ID.Segment_ID = Track.Segment_ID
),

注意:我使用过Road.,但您可能需要使用Segment_ID

编辑,对于第二个错误,您需要在 中包含segment_id并且roads_by_segment您似乎没有Segment_idby_segmentCTE 中选择 :

with by_segment as 
(
    SELECT  
        Segment_ID.Segment_ID, 
        Road.Road_ID,
        GEOMETRY::STGeomFromText(Track.Track.STAsText(),4326) as the_geom,
        row_number() over (partition by Road.road_id order by Segment_ID.Segment_Id) as [rn],
        count(*) over (partition by Road.road_id) as [c]
    FROM dbo.Road
    LEFT JOIN Segment_ID  ON Road.Road_ID = Segment_ID.Road_ID
    LEFT JOIN Track ON Segment_ID.Segment_ID = Track.Segment_ID
),
roads_by_segment as 
(
    select 
       road_id,
       the_geom,
       [rn],
       [c],
       [segment_id]
    from by_segment
    where [rn] = 1
    union all
    select 
       [a].road_id,
       [a].the_geom.STUnion([b].the_geom),
       [b].[rn],
       [b].[c],
       [b].[segment_id]
    from by_segment as [a]
    inner join roads_by_segment as [b]
       on [a].segment_id = b.[segment_id]
       and [a].[rn] = [b].[rn]+1
)

select * from roads_by_segment where [rn] = [c]
于 2013-02-21T18:36:32.343 回答
1

如果列存在于多个表中,则需要用表名限定列。在 row_number 函数中指定 Road.Road_ID 或 Segment_ID.Road_ID。由于您正在执行 LEFT JOIN,因此您想使用 Road.Road_ID。Segment_ID.Segment_ID 或 Track.Segment_ID 也是如此

于 2013-02-21T18:36:52.553 回答