2013-03-06 63 views
4

我正在執行查詢,該查詢在具有多個值的where子句中有多個列。我知道在SQL中可以使用IN條件來滿足並獲得正確的輸出。 teradata有什麼辦法?多個列在where子句中有多個值

我在Oracle中的代碼如下所示:

select td.country_code,td.phone_num 
from telephone_directory td 
where (td.country_code, td.phone_num) in ((91,1234567890),(44,1020304050),(1,998877446655)) 

此打印出即3行

我在Teradata的查詢看起來像這樣

select country_code ,phone_num 
from telephone_directory 
where (country_code in (91, 44, 1) and phone_num in(1234567890, 1020304050, 998877446655) 

然而,這將返回更精確的結果行:

country_code phone_num 
91   1234567890 
91   1020304050 
44   1020304050 
1    998877446655 

注意:country_code和phone號碼的組合不是唯一的。

有沒有一種方法可以像在ORACLE中那樣在Teradata中進行過濾?

+0

頂部查詢使用「兩兩比較的子查詢」(資料來源:2007年的Oracle SQL類幻燈片)。這意味着這兩個值必須存在於同一行中。第二個查詢是非成對比較,兩個值可以彼此存在於不同的行中。 由於Teradata無法進行配對比較,所以最好的答案可能是下面顯示的將值連接成組合鍵的那個。 – Bryansix 2017-01-10 02:17:32

回答

1

從邏輯上看,您看到的Teradata的結果是正確的。你有一個電話號碼和多個國家代碼。下面的SQL應該產生的結果,你想看看:

select td.country_code,td.phone_num 
from telephone_directory td 
where (td.country_code, td.phone_num) 
    in (SELECT 91 AS country_code_ 
      , 1234567890 AS phone_num_ 
     UNION 
     SELECT 44 AS country_code_ 
      , 1020304050 as phone_num_ 
     UNION 
     SELECT 1 as country_code_ 
      , 998877446655 as phone_num_ 
    ); 

這也可以使用WITH子句或組合,並用括號組合在一起,以產生正確的結果語句重新編寫。

3

據我所知,Teradata不支持Oracle擴展where子句的語法;你需要爲複合表達式指定條件:

select country_code ,phone_num 
from telephone_directory 
where (country_code=91 and phone_num=1234567890) 
    or (country_code=44 and phone_num=1020304050) 
    or (country_code=1 and phone_num=998877446655) 
+0

同意 - Teradata不支持「擴展的」WHERE子句語法。 +1 – 2013-03-07 03:40:11

2
select USER_TYPE,USER_ID 
from USER_TABLE 
where (USER_TYPE || USER_ID) in (('F6713'),('S1178'),('M5715'),('F8341'),('F1284')) 
+0

是什麼?你從哪裏得到這些價值? – 2015-04-22 19:36:21