처음에는 2 임시 테이블을 만든 다음 내 DB의 기존 테이블을 쿼리하고이 테이블을 하위 쿼리에 조인 한 다음 마지막으로 1 조인 임시 테이블. 이 작업을 수행 할 때 기존 테이블에서 조인 할 키를 바인딩 할 수 없다는 오류가 발생합니다. 이상한 점은 하위 쿼리에 대한 모든 참조를 꺼내서 기존 테이블과 임시 테이블이 포함 된 쿼리 만 남기고 기존 테이블을 하위 SUery에 조인하면 정상적으로 작동한다는 것입니다.테이블, 임시 테이블, 하위 쿼리 조인 문제가있는 쿼리
그러나 3을 모두 입력하면 "다중 부분으로 식별 된 z [currnecy 키]를 바인딩 할 수 없습니다."라는 메시지가 표시됩니다.이 키는 기존 테이블에 있으므로 이상한 것으로 보이며 조인합니다 임시 테이블이나 하위 쿼리만으로도 충분하지만 둘 다 함께 사용할 수는 없습니다.
하위 쿼리에 가입하는 데 문제가 있습니다. 그러나이 상황에서는 문제가 하위 쿼리와 임시 테이블에 같은 쿼리에서 합류하는 것으로 보입니다.이 쿼리는 해결 방법이 확실하지 않습니다.
코드는 다음과 같습니다.
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]
여기에서 문제의 일부는 두 개의 다른 조인 구문을 혼합하여 이해하기가 어려울 수 있다는 것입니다. 일부에서는 ANSI 표준'INNER'와 'LEFT' 조인을 사용하고 있지만 앨리어스'q'가있는 인라인 뷰는 쉼표로 구분 된 테이블 목록의 일부로 조인됩니다. – ninesided
또한 SQL Fiddle에 대한 대표적인 예를 설정할 수 있습니까? 테이블이 무엇인지 모를 때 문제를 발견하는 것은 어렵습니다. – ninesided