查询中的一些错误

时间:2013-02-21 18:35:25

标签: sql sql-server

我是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".

2 个答案:

答案 0 :(得分: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_id CTE中的by_segment

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]

答案 1 :(得分:1)

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

也是如此