【问题标题】:Teradata SQL - Conditions depending on conditions in WHERE clauseTeradata SQL - 条件取决于 WHERE 子句中的条件
【发布时间】:2018-08-07 20:45:36
【问题描述】:

这个问题是旧帖子的变体

条件

如果 Customer_country = ‘A’

  1. 那么 Ship_country 必须 = ‘A’

  2. 和 Customer_number ‘A2’(即排除 A2)

  3. 并且日期在 x 和 y 之间。 (所有客户所在国家/地区相同)

相同的逻辑适用于所有 Customer_country,其中排除了相应的 B2、C2、D2 Customer_number。

我对使用 CASE...WHEN 感到困惑,因为它应该返回一个值。
但我没有返回任何价值。请帮助解决这个问题。

【问题讨论】:

    标签: sql teradata where-clause case-when


    【解决方案1】:

    您应该使用where 子句而不是case when

    使用|| 方法将字符组合为A2B2 ... ,然后您可以排除它。

    SELECT *
    FROM master as t1 
    WHERE 
        t1.Customer_number <> t1.Customer_Country || '2'
    AND 
        t1.Ship_Country = t1.Customer_Country
    

    teradata ||

    编辑

    如果 customer_number 类似于 '981432776',您可以使用NOT IN 排除。

    SELECT *
    FROM master as t1 
    WHERE 
        t1.Customer_number NOT IN ('A2','B2','C2','D2')
    AND 
        t1.Ship_Country = t1.Customer_Country
    

    【讨论】:

    • A、B、C、D、A1、A2...等是实际国家和客户编号的替代字母。 'A2' 可能是一个 customer_number,如 '981432776'。我假设条件遵循 WHERE 子句。
    猜你喜欢
    • 2018-08-06
    • 2021-03-10
    • 1970-01-01
    • 2016-10-16
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多