SQL Case 式の構文?
-
08-06-2019 - |
質問
とは何ですか 完了 SQL Case 式の構文は正しいですか?
解決
の 完了 構文は、使用しているデータベース エンジンによって異なります。
SQL サーバーの場合:
CASE case-expression
WHEN when-expression-1 THEN value-1
[ WHEN when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
または:
CASE
WHEN boolean-when-expression-1 THEN value-1
[ WHEN boolean-when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
表現など:
case-expression - something that produces a value
when-expression-x - something that is compared against the case-expression
value-1 - the result of the CASE statement if:
the when-expression == case-expression
OR the boolean-when-expression == TRUE
boolean-when-exp.. - something that produces a TRUE/FALSE answer
リンク: CASE (Transact-SQL)
また、WHEN ステートメントの順序が重要であることにも注意してください。重複する複数の WHEN 句を簡単に作成できます。 最初に一致したものが使用されます.
注記:ELSE 句が指定されておらず、一致する WHEN 条件が見つからない場合、CASE 式の値は次のようになります。 ヌル.
他のヒント
複数の商品をタグ付けしたことを考慮すると、 満杯 正しい構文は、ISO/ANSI SQL-92 標準にある構文です。
<case expression> ::=
<case abbreviation>
| <case specification>
<case abbreviation> ::=
NULLIF <left paren> <value expression> <comma>
<value expression> <right paren>
| COALESCE <left paren> <value expression>
{ <comma> <value expression> }... <right paren>
<case specification> ::=
<simple case>
| <searched case>
<simple case> ::=
CASE <case operand>
<simple when clause>...
[ <else clause> ]
END
<searched case> ::=
CASE
<searched when clause>...
[ <else clause> ]
END
<simple when clause> ::= WHEN <when operand> THEN <result>
<searched when clause> ::= WHEN <search condition> THEN <result>
<else clause> ::= ELSE <result>
<case operand> ::= <value expression>
<when operand> ::= <value expression>
<result> ::= <result expression> | NULL
<result expression> ::= <value expression>
構文規則
1) NULLIF (V1, V2) is equivalent to the following <case specification>:
CASE WHEN V1=V2 THEN NULL ELSE V1 END
2) COALESCE (V1, V2) is equivalent to the following <case specification>:
CASE WHEN V1 IS NOT NULL THEN V1 ELSE V2 END
3) COALESCE (V1, V2, . . . ,n ), for n >= 3, is equivalent to the
following <case specification>:
CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,n )
END
4) If a <case specification> specifies a <simple case>, then let CO
be the <case operand>:
a) The data type of each <when operand> WO shall be comparable
with the data type of the <case operand>.
b) The <case specification> is equivalent to a <searched case>
in which each <searched when clause> specifies a <search
condition> of the form "CO=WO".
5) At least one <result> in a <case specification> shall specify a
<result expression>.
6) If an <else clause> is not specified, then ELSE NULL is im-
plicit.
7) The data type of a <case specification> is determined by ap-
plying Subclause 9.3, "Set operation result data types", to the
data types of all <result expression>s in the <case specifica-
tion>.
Access Rules
None.
General Rules
1) Case:
a) If a <result> specifies NULL, then its value is the null
value.
b) If a <result> specifies a <value expression>, then its value
is the value of that <value expression>.
2) Case:
a) If the <search condition> of some <searched when clause> in
a <case specification> is true, then the value of the <case
specification> is the value of the <result> of the first
(leftmost) <searched when clause> whose <search condition> is
true, cast as the data type of the <case specification>.
b) If no <search condition> in a <case specification> is true,
then the value of the <case expression> is the value of the
<result> of the explicit or implicit <else clause>, cast as
the data type of the <case specification>.
ここにあります CASE
PostgreSQL ドキュメントのステートメントの例 (ここでは、Postgres は SQL 標準に従っています):
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;
または
SELECT a,
CASE a WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'other'
END
FROM test;
1 つのフィールドを可能な値のリストと照合するだけの場合は、2 番目の形式の方が明確です。最初の形式では、より複雑な式が可能になります。
サイベースにも同じようなものがある case 構文 SQL サーバーとして:
説明
条件付き SQL 式をサポートします。値式を使用できる場所ならどこでも使用できます。
構文
case
when search_condition then expression
[when search_condition then expression]...
[else expression]
end
大文字と小文字の区別と値の構文
case expression
when expression then expression
[when expression then expression]...
[else expression]
end
パラメーター
場合
case 式が始まります。
いつ
検索条件または比較する式の前に置きます。
検索条件
選択された結果の条件を設定するために使用されます。case 式の検索条件は、where 句の検索条件と似ています。検索条件の詳細については、『Transact-SQL ユーザー ガイド』を参照してください。
それから
case の結果値を指定する式の前に置きます。
表現
列名、定数、関数、サブクエリ、あるいは算術演算子やビット演算子で接続された列名、定数、関数の任意の組み合わせです。式の詳細については、「式」を参照してください。
例
select disaster,
case
when disaster = "earthquake"
then "stand in doorway"
when disaster = "nuclear apocalypse"
then "hide in basement"
when monster = "zombie apocalypse"
then "hide with Chuck Norris"
else
then "ask mom"
end
from endoftheworld
Oracle の同じページを探してみたところ、これは同じ構文のようですが、説明が少し異なるだけです。
オラクル 11g ドキュメントの構文:
CASE { simple_case_expression | searched_case_expression }
[ else_clause ]
END
simple_case_expression
expr { WHEN comparison_expr THEN return_expr }...
検索されたケースの式
{ WHEN condition THEN return_expr }...
else_clause
ELSE else_expr
Oracle の場合、注意すべき点が 1 つあります。 when が一致せず、else 部分が存在しない場合、例外が発生します。
SQL SERVER の Case ステートメント構文:
CASE column
WHEN value1 THEN 1
WHEN value3 THEN 2
WHEN value3 THEN 3
WHEN value1 THEN 4
ELSE ''
END
また、以下のように使用することもできます。
CASE
WHEN column=value1 THEN 1
WHEN column=value3 THEN 2
WHEN column=value3 THEN 3
WHEN column=value1 THEN 4
ELSE ''
END