【发布时间】:2020-07-13 13:46:45
【问题描述】:
我不断收到错误消息
消息 8156,第 16 级,状态 1,第 5 行
为“sv2017”多次指定了“Id”列。消息 8156,第 16 层,状态 1,第 7 行
为“sv2010”多次指定了“Id”列。
使用以下脚本:
select
sv2017.id, sv2017.TotalArea, sv2017.uarn,
sv2010.id, sv2010.PlantMachineryValue, sv2017.PlantMachineryValue,
sv2010.uarn, sv2017.BAReferenceNumber
from
(select *
from summaryvaluation sv
join SummaryValuationPlant svp on sv.id = svp.SummaryValuationId
where sv.yearid = 2
and sv.id in (select id /*Get the most recent valuation for the list year*/
from
(select
id, uarn,
rank() over (partition by uarn order by fromdate desc) as RankOrder
from
summaryvaluation
where
yearid = 2 and systemactive = 1) tbl
where
rankorder = 1)) sv2017
left outer join
(select *
from summaryvaluation sv2
join SummaryValuationPlant svp2 on sv2.id = svp2.SummaryValuationId
where sv2.yearid = 1
and sv2.id in (select id /*Get the most recent valuation for the list year*/
from
(select
id, uarn,
rank() over (partition by uarn order by fromdate desc) as RankOrder
from
summaryvaluation
where
yearid = 1 and systemactive = 1) tbl
where
rankorder = 1)) sv2010 on sv2017.uarn = sv2010.uarn
where
isnull (sv2017.PlantMachineryValue, 0) > isnull(sv2010.PlantMachineryValue, 0)
而且我看不出哪里出错了!这可能很明显,我的大脑无法正常工作,但感谢您提供任何帮助。
谢谢
【问题讨论】:
-
错误很明显
-
这只是为什么你不应该只是“拍”
SELECT *的原因之一。显然,summaryvaluation和SummaryValuationPlant表都有一个列id,因此SELECT(在子查询中)将返回id列和id列;你看到那里的问题了吗? -
使用别名,例如
T1.id As Foo, T2.id As BarSQL Server 需要知道你指的是哪个id