Teradata SQL - Conditions depending on conditions in WHERE clause


Keywords:sql 


Question: 

This question is a variation on an older post
enter image description here

The conditions

If Customer_country = ‘A’

  1. then Ship_country must = ‘A’

  2. and Customer_number <> ‘A2’ (i.e. exclude A2)

  3. and date is between x and y. (same for all Customer country)

The same logic goes for all Customer_country, where respective B2, C2, D2 Customer_numbers are excluded.

enter image description here

I am confused with using CASE…WHEN because it is expected to return a value.
But I am not returning any value. Please help with this query.


1 Answer: 

You should use where clause instead of case when

Using || method to combine char like A2 or B2 ... ,then you can exclude that.

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

teradata ||

EDIT

if the customer_number is like '981432776',you can use NOT IN to exclude that.

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