我有一个查询,我在开始时创建2个临时表,然后我查询我的数据库中的现有表并将此表连接到子查询,然后最后连接到其中一个临时表。当我这样做时,我得到一个错误,我从现有表加入的密钥不能绑定。奇怪的是,如果我取出对子查询的所有引用,只留下查询与现有的表和它加入的临时表,并且如果我将现有的表联合到子suery它就可以正常工作。
但是当我尝试将所有3个放在一起时,它给了我“多部分识别的z。[currnecy key]无法绑定”,这似乎是一个奇怪的错误,因为这个键在现有的表中并加入单独使用临时表或子查询,但不能同时使用。
我知道加入子查询的问题,但在这种情况下似乎问题似乎是在同一查询中加入子查询和临时表,我不知道如何解决。< / p>
代码如下。
declare @tmpFx table (currency_key int, effective_date_key int, expiration_date_key int, to_usd float, from_usd float ) --primary key (currency_key, date_key))
insert into @tmpFx(currency_key, effective_date_key, expiration_date_key, to_usd, from_usd)
select [currency key], cast(convert(char(8),[effective date],112) as int), cast(convert(char(8),[expiration date],112) as int), [to usd], [from usd]
from v_fx --where [effective date] >= @beginDate
declare @fixedFx table (currency_key int, to_usd float, from_usd float primary key (currency_key))
insert into @fixedFx(currency_key, to_usd, from_usd)
select [currency key], [to usd], [from usd]
from v_fx where [effective date] = '2012-01-01'
select z.[currency key], --stat_fx.to_usd to_usd, stat_fx.from_usd from_usd, --q.*,--
stat_usd_amt2 = case when z.[currency key] = 100001 then q.orig_amt else 0 end --sum(q.orig_amt * stat_fx.to_usd)
from [dim country] z,
(select b.country_key, a.currency_key, a.data_type_key, sum(a.amount) orig_amt,
sum(a.amount * stat_fx.to_usd) stat_usd_amt,
sum((a.amount * stat_fx.to_usd) * stat_fx.from_usd) home_curr_amt
from tbl_cohort a
inner join tbl_management_code b on a.management_code = b.management_code
left outer join @tmpFx stat_fx on a.currency_key = stat_fx.currency_key
where a.data_type_key = 1
and a.date_key > 20111231
group by b.country_key, a.currency_key, a.data_type_key) q
inner join @tmpFx stat_fx on z.[currency key] = stat_fx.currency_key
where q.[country_key]= z.[country key]
答案 0 :(得分:1)
我相信这是因为你在旧样式和新样式之间混合连接格式(如建议的那样)。我能够用我自己的数据模拟一个类似的问题,并得到与未绑定标识符相同的错误。请改为尝试以下内容。
declare @tmpFx table (currency_key int, effective_date_key int, expiration_date_key int, to_usd float, from_usd float)
insert into @tmpFx(currency_key, effective_date_key, expiration_date_key, to_usd, from_usd)
select [currency key], cast(convert(char(8),[effective date],112) as int), cast(convert(char(8),[expiration date],112) as int), [to usd], [from usd]
from v_fx
declare @fixedFx table (currency_key int, to_usd float, from_usd float primary key (currency_key))
insert into @fixedFx(currency_key, to_usd, from_usd)
select [currency key], [to usd], [from usd]
from v_fx where [effective date] = '2012-01-01'
select z.[currency key],
case when z.[currency key] = 100001 then q.orig_amt else 0 end AS stat_usd_amt2
from [dim country] z
JOIN (
select b.country_key, a.currency_key, a.data_type_key, sum(a.amount) AS orig_amt,
sum(a.amount * stat_fx.to_usd) as stat_usd_amt,
sum((a.amount * stat_fx.to_usd) * stat_fx.from_usd) as home_curr_amt
from tbl_cohort a
join tbl_management_code b
on a.management_code = b.management_code
left join @tmpFx stat_fx
on a.currency_key = stat_fx.currency_key
where a.data_type_key = 1
and a.date_key > 20111231
group by b.country_key, a.currency_key, a.data_type_key
) q
ON q.[country_key] = z.[country_key]
join @tmpFx stat_fx
on z.[currency key] = stat_fx.currency_key
当我离开你的第二个临时表(@fixedFx)时,你可能想要删除它,如果你没有计划使用它的数据。