Using CASE statements
Like any other programming language, Teradata SQL provides if-then-else logic using the CASE
statement. It helps in fetching alternate values for a column based on the condition specified in the expression. If any one of the values gets qualified as TRUE
, its value gets captured and the counter goes on until all the rows have been processed.
Every CASE
statement needs to have a pair of WHEN
and THEN
statements and to be closed using an END
statement.
The syntax of CASE
is as follows:
The syntax of case statement
Let's put this into a code statement as follows:
/*CASE SELECT*/ SELECT EMP_name, Month, CASEWHENMONTH = 'JUNE'THEN'yes'ELSENULLENDAS SUMMER FROM Table_CASE
Let's understand this by means of a flow chart:
Decoding the statement:
- The CASE 1 statement checks each row to see if the conditional statement is met.
- For any given row for CASE 1, if that conditional statement is true, the counter for the true condition is increased or the corresponding value gets stored.
- In...