Sei sulla pagina 1di 1

Chapter 3: Selecting 107

ELSE <expression> is evaluated and returned; if there is no ELSE <expres-


sion>, then NULL is returned.
Here is an example that uses a searched CASE expression to specify three
WHEN conditions that use AND and IN as well as simple comparisons. A sec-
ond basic CASE expression is also used to translate the result of the first
expression into a string title.
SELECT CASE
WHEN sales_rep = 129
AND region = 'Western'
THEN 1
WHEN region = 'Western'
THEN 2
WHEN region IN ( 'Eastern', 'Central' )
THEN 3
ELSE 0
END AS sort_order,
CASE sort_order
WHEN 1 THEN 'Western 129'
WHEN 2 THEN 'Other Western'
WHEN 3 THEN 'Eastern and Central'
END AS breakdown,
COUNT(*) AS orders
FROM sales_order
WHERE sort_order > 0
GROUP BY sort_order
ORDER BY sort_order;
Heres what the result looks like using the ASADEMO database:
sort_order breakdown orders
========== ============= ======
1 Western 129 6
2 Other Western 55
3 Eastern and Central 468

3.11 Top 15 Scalar Built-in Functions


Function calls fall into four categories. First, there are references to user-defined
functions created with the CREATE FUNCTION statement. Second, there are
ordinary built-in functions like ABS() and SUBSTRING(), which look a lot like
functions available in other languages. Third, there are a handful of special
built-in functions, like CAST() and NUMBER(*), which work like ordinary
built-in functions but have some unusual syntax in the argument lists. And
finally, there are the aggregate built-in functions, which are in a whole world by
themselves.
<function_call> ::= <user_defined_function_call> -- scalar function
| <ordinary_builtin_function_call> -- scalar function
| <special_builtin_function_call> -- scalar function
| <aggregate_builtin_function_call> -- aggregate function
<user_defined_function_call> ::= <user_defined_function_name>
"(" [ <function_argument_list> ] ")"
<user_defined_function_name> ::= <identifier>
<function_argument_list> ::= <expression> { "," <expression> }
<ordinary_builtin_function_call> ::= <ordinary_builtin_function_name>
"(" [ <function_argument_list> ] ")"
<ordinary_builtin_function_name> ::= <identifier>
<special_builtin_function_call> ::= CAST "(" <expression> AS <data_type> ")"

Potrebbero piacerti anche