2013-02-21 69 views
1

我有一個查詢,我在開始創建2個臨時表,然後我查詢我的數據庫中的現有表,然後將此表連接到子查詢,然後最後連接到1的臨時表。當我這樣做時,我得到一個錯誤,即我現在加入的密鑰不能被綁定。奇怪的是,如果我取出對子查詢的所有引用,並將查詢與現有表以及它所加入的臨時表保持一致,並且如果我將現有表聯合到子查詢中,則它工作得很好。查詢與表,臨時表,子查詢連接問題

但是,當我嘗試將所有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] 
+1

這裏的部分問題可能是您混合了兩種不同的連接語法,這使得它很難理解。您正在使用ANSI標準'INNER'和'LEFT'加入一些,但帶有別名'q'的內聯視圖作爲以逗號分隔的表列表的一部分加入。 – ninesided 2013-02-21 22:34:14

+0

另外,你能在SQL Fiddle上設置一個代表性的例子嗎?當我們不知道表格是什麼時,很難發現問題。 – ninesided 2013-02-21 22:45:29

回答

1

我相信這是因爲你正在舊式和新式之間混合連接格式(建議使用ninesided)。我能用自己的數據模擬出類似的問題,並得到了與未綁定標識符相同的錯誤。改爲嘗試以下方法。

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),你可能想,如果你沒有使用其數據的所有計劃將其刪除。