How to write nested Case statement in Teradata?

Case Statement in Teradata

CASE statement is a conditional expression that used to evaluate the conditions or perform the equality comparisons against column values in Teradata. It goes through each condition and returns the value when the condition is met( Like an IF-THEN-ELSE-Statement).

If no condition are TRUE in Case statement, It returns the value defined by an optional ELSE clause, or if omitted, NULL.

Syntax for Case Statement

Nested Case Statement in Teradata

Teradata allows to add one case statement inside the another case statement. The nested case statement helps to validate the multiple criteria under one WHEN condition.The inner case statement requires their own END statement.

Syntax for Nested Case Statement

Examples for Case and Nested case statement

Lets see the case and nested case statements with examples in Teradata

The Government giving scholarship for the student’s higher education. The scholarship amount will vary depends on the education streams and the fees amount. Here we are going to use the case and nested case statements to calculate the scholarship amount.

Student Table in Teradata
Student Table in Teradata

Example : Case statement

  • If the Higher studies is Medical, scholarship amount is 50000
  • If the Higher studies is Engineering, scholarship amount is 40000
  • For other Higher studies, scholarship amount is 25000

Output

Case when statement example in Teradata
Case when statement example in Teradata

Example : Nested Case statement

  • If the Higher studies is Medical, scholarship amount will vary depends on the College fees as below
    • If the College fees is less than 200000, scholarship amount is 50000
    • If the College fees is less than 300000 , scholarship amount is 75000
    • If the College fees is more than 300000, scholarship amount is 100000
  • If the Higher studies is Engineering, scholarship amount will vary as below
    • If the College fees is less than 150000 , scholarship amount is 40000
    • If the College fees is less than 250000 , scholarship amount is 60000
    • If the College fees is more than 250000 , scholarship amount is 80000
  • For other higher studies, scholarship amount will be 25000

Output

Multiple Case statement example in Teradata
Multiple Case statement example in Teradata

Recommended Articles