Sunteți pe pagina 1din 3

Classifying Results with SQL CASE Statements

SQL includes a CASE statement that allows you to return varying results based upon the evaluation of expressions. You
can use a CASE statement anywhere within a SQL statement that you would normally include an expression. CASE
statements are often found in SELECT, UPDATE and DELETE statements as well as WHERE and IN clauses.
CASE Statement Syntax
There are two different types of CASE statements. The simple CASE statement takes an input value and compares it to a
list of expressions to select the appropriate output value. The advanced CASE statement evaluates each of a set of
expressions to select the appropriate output value.
The format of the simple CASE statement is:

CASE input
WHEN test THEN result
ELSE result
END

If the input expression matches a test value, the CASE statement evaluates to the corresponding result. If the input
expression does not match any of the test values, the CASE statement evaluates to the ELSE clause’s result.

The format of the advanced (Searched)CASE statement is:

CASE
WHEN test THEN result
ELSE result
END

As with the basic CASE statement, you may include as many WHEN clauses as you wish. Each of the test expressions must
be a Boolean expression (meaning it evaluates to either true or false). When the database encounters the CASE statement
it begins walking through the WHEN clauses in order, testing the Boolean expressions. The CASE statement returns the
result expression corresponding to the first test expression that evaluates to true. If no test expression is true, the CASE
statement returns the result expression from the ELSE clause.
SELECT CASE ("column_name")
WHEN "value1" THEN "result1"
WHEN "value2" THEN "result2"
...
[ELSE "resultN"]
END
FROM "table_name";
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
Table Geography
Region_Name Store_Name
East Boston
East New York
West Los Angeles
West San Diego
Examples:
SELECT Store_Name, CASE Store_Name
WHEN 'Los Angeles' THEN Sales * 2
WHEN 'San Diego' THEN Sales * 1.5
ELSE Sales
END
"New Sales",
Txn_Date
FROM Store_Information;

SELECT Store_Name, Txn_Date, CASE


WHEN Sales >= 1000 THEN 'Good Day'
WHEN Sales >= 500 THEN 'OK Day'
ELSE 'Bad Day'
END
"Sales Status"
FROM Store_Information;

SELECT first_name, last_name, weight_class =


CASE
WHEN weight<172 THEN 'Welterweight'
WHEN weight<=192 THEN 'Middleweight'
WHEN weight<=214 THEN 'Light heavyweight'
WHEN weight<=220 THEN 'Cruiserweight'
ELSE 'Heavyweight'
END
FROM athletes

Subquery
It is possible to embed a SQL statement within another. When this is done on the WHERE or the HAVING statements, we
have a subquery construct.
Syntax:
SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE "condition");
[Comparison Operator] could be equality operators such as =, >, <, >=, <=. It can also be a text operator such as "LIKE".
The lower SQL statement is considered as the "inner query", while the upper is considered as the "outer query".

Example:
SELECT SUM (Sales) FROM Store_Information
WHERE Store_Name IN
(SELECT Store_Name FROM Geography
WHERE Region_Name = 'West');

Result?

The inner query is first executed, and the result is then fed into the outer query. This type of subquery is called a simple
subquery.
If the inner query is dependent on the outer query, we will have a correlated subquery.
Example:
SELECT SUM (a1.Sales) FROM Store_Information a1
WHERE a1.Store_Name IN
(SELECT Store_Name FROM Geography a2
WHERE a2.Store_Name = a1.Store_Name);
Notice the WHERE clause in the inner query, where the condition involves a table from the outer query.

SQL String Function Concatenate


Combine together (concatenate) the results from several different fields.
Syntax:
CONCAT (str1, str2, str3, ...)
Example:
SELECT CONCAT(Region_Name, Store_Name) FROM Geography
WHERE Store_Name = 'Boston';

Result?

S-ar putea să vă placă și