Tsql 查询表、临时表、子查询连接问题
我有一个查询,首先创建两个临时表,然后查询数据库中的一个现有表,并将该表连接到一个子查询,最后连接到一个临时表。当我这样做时,我会得到一个错误,即我从现有表连接的键无法绑定。奇怪的是,如果我去掉对子查询的所有引用,只保留查询和它所连接的现有表和临时表,并且如果我将现有表和子查询连接起来,它就可以正常工作 但当我尝试将这三个键放在一起时,它给了我一个“无法绑定由多个部分标识的z.[currency key]的错误”,这似乎是一个奇怪的错误,因为该键位于现有表中,并且仅与临时表或子查询很好地连接,但不能同时连接在一起 我知道关于子查询连接的问题,但在这种情况下,问题似乎是连接到同一查询中的子查询和临时表,我不知道如何解决 代码如下Tsql 查询表、临时表、子查询连接问题,tsql,sql-server-2005,subquery,inner-join,temp-tables,Tsql,Sql Server 2005,Subquery,Inner Join,Temp Tables,我有一个查询,首先创建两个临时表,然后查询数据库中的一个现有表,并将该表连接到一个子查询,最后连接到一个临时表。当我这样做时,我会得到一个错误,即我从现有表连接的键无法绑定。奇怪的是,如果我去掉对子查询的所有引用,只保留查询和它所连接的现有表和临时表,并且如果我将现有表和子查询连接起来,它就可以正常工作 但当我尝试将这三个键放在一起时,它给了我一个“无法绑定由多个部分标识的z.[currency key]的错误”,这似乎是一个奇怪的错误,因为该键位于现有表中,并且仅与临时表或子查询很好地连接,但
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]
我相信这是因为您正在混合旧样式和新样式之间的连接格式(正如Nineside所建议的)。我能够用自己的数据模拟一个类似的问题,并且得到了关于未绑定标识符的相同错误。试试下面的方法
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)中留下了它,但如果您根本没有使用它的数据的计划,您可能会想删除它。我相信这是因为您在旧样式和新样式之间混合了连接格式(如Ninedesided所建议的)。我能够用自己的数据模拟一个类似的问题,并且得到了关于未绑定标识符的相同错误。试试下面的方法
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)中留下了它,但如果您根本没有使用它的数据的计划,您可能会想删除它。这里的部分问题可能是您混合了两种不同的连接语法,这使您很难理解。您正在使用ANSI标准的
internal
和LEFT
联接,但是别名为q
的内联视图作为逗号分隔的表列表的一部分联接。此外,您能否在SQL FIDLE上设置一个代表性的示例?当我们不知道表是什么时,很难发现问题。这里的部分问题可能是你混合了两种不同的连接语法,这使得它很难理解。您正在使用ANSI标准的internal
和LEFT
联接,但是别名为q
的内联视图作为逗号分隔的表列表的一部分联接。此外,您能否在SQL FIDLE上设置一个代表性的示例?当我们不知道桌子是什么的时候,很难发现问题。