Example: Using a CASE expression to Enhance Performance

Teradata Vantageā„¢ SQL Functions, Expressions, and Predicates

brand
Software
Teradata Vantage
prodname
Teradata Database
Teradata Vantage NewSQL Engine
vrm_release
16.20
category
Programming Reference
featnum
B035-1145-162K

The following example shows how using a CASE expression can result in significantly enhanced performance by eliminating multiple passes over the data. Without using CASE, you would have to perform multiple queries for each region and then consolidate the answers to the individual queries in a final report.

   SELECT SalesMonth, SUM(CASE 
                           WHEN Region='NE' 
                           THEN Revenue 
                           ELSE 0 
                          END),
                      SUM(CASE 
                           WHEN Region='NW' 
                           THEN Revenue 
                           ELSE 0 
                          END),
                      SUM(CASE 
                           WHEN Region LIKE 'N%' 
                           THEN Revenue 
                           ELSE 0 
                          END)
   AS NorthernExposure, NorthernExposure/SUM(Revenue),
   SUM(Revenue)
   FROM Sales
   GROUP BY SalesMonth;