【问题标题】:multiple columns with multiple values in where clausewhere 子句中具有多个值的多个列
【发布时间】:2015-04-23 04:54:06
【问题描述】:

我正在执行一个查询,该查询在 where 子句中有多个列,其中包含多个值。我知道在 SQL 中,您可以使用 IN 条件来满足并获得正确的输出。 teradata的方法是什么?

我在 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 和电话号码的组合不是唯一的。

有没有办法像 ORACLE 那样在 teradata 中过滤掉它?

【问题讨论】:

  • 顶部查询使用“成对比较子查询”(来源:2007 Oracle SQL 类幻灯片)。这意味着这两个值必须存在于同一行中。第二个查询是非成对比较,两个值可以彼此存在于不同的行中。由于 Teradata 无法进行成对比较,最好的答案可能是下面显示的将值连接成组合键的答案。

标签: teradata


【解决方案1】:

据我所知,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)

【讨论】:

  • 同意 - Teradata 不支持“扩展的”WHERE 子句语法。 +1
【解决方案2】:
select USER_TYPE,USER_ID
from USER_TABLE
where (USER_TYPE || USER_ID) in (('F6713'),('S1178'),('M5715'),('F8341'),('F1284'))

【讨论】:

  • 什么?你从哪里得到这些值?
【解决方案3】:

从逻辑上讲,您看到的 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 子句或与括号组合在一起的 AND 语句的组合来重写,以产生正确的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-06
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多