This section describes the <acronym>SQL</acronym>-compliant conditional expressions available in <productname>PostgreSQL</productname>. 本節ではPostgreSQLで使用可能なSQL準拠の条件式について説明します。
If your needs go beyond the capabilities of these conditional expressions, you might want to consider writing a server-side function in a more expressive programming language. ここで説明する条件式より発展した機能を求める場合は、より表現の豊富なプログラム言語でストアドプロシージャを記述することで解決されます。
Although <token>COALESCE</token>, <token>GREATEST</token>, and
<token>LEAST</token> are syntactically similar to functions, they are
not ordinary functions, and thus cannot be used with explicit
<token>VARIADIC</token> array arguments.
COALESCE、GREATEST、LEASTは構文的には関数に似ていますが通常の関数ではなく、明示的なVARIADIC配列引数と一緒には使えません。
CASE #
The <acronym>SQL</acronym> <token>CASE</token> expression is a
generic conditional expression, similar to if/else statements in
other programming languages:
SQLのCASE式は他のプログラミング言語のif/else構文に類似した汎用条件式です。
CASE WHENconditionTHENresult[WHEN ...] [ELSEresult] END
<token>CASE</token> clauses can be used wherever
an expression is valid. Each <replaceable>condition</replaceable> is an
expression that returns a <type>boolean</type> result. If the condition's
result is true, the value of the <token>CASE</token> expression is the
<replaceable>result</replaceable> that follows the condition, and the
remainder of the <token>CASE</token> expression is not processed. If the
condition's result is not true, any subsequent <token>WHEN</token> clauses
are examined in the same manner. If no <token>WHEN</token>
<replaceable>condition</replaceable> yields true, the value of the
<token>CASE</token> expression is the <replaceable>result</replaceable> of the
<token>ELSE</token> clause. If the <token>ELSE</token> clause is
omitted and no condition is true, the result is null.
CASE句は式が有効な位置であればどこでも使用可能です。
それぞれのconditionとはboolean型の結果を返す式です。
もしconditionの結果が真であれば、CASE式の値は、conditionに続くresultとなります。そして、CASE式の残りは処理されません。
もしconditionの結果が偽であれば後に続く全てのWHEN句が同じようにして調べられます。
WHENのconditionの1つも真でない場合、CASE式の値はELSE句のresultになります。
ELSE句が省略され、どのconditionも真でない場合、結果はNULLです。
An example: 以下に例を示します。
SELECT * FROM test;
a
---
1
2
3
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;
a | case
---+-------
1 | one
2 | two
3 | other
The data types of all the <replaceable>result</replaceable>
expressions must be convertible to a single output type.
See <xref linkend="typeconv-union-case"/> for more details.
全てのresult式のデータ型は単一の出力型に変換可能でなければなりません。
詳細は10.5を参照してください。
There is a <quote>simple</quote> form of <token>CASE</token> expression
that is a variant of the general form above:
以下のように、上記の一般的な形式と異なるCASE式の「単純な」形式が存在します。
CASEexpressionWHENvalueTHENresult[WHEN ...] [ELSEresult] END
The first
<replaceable>expression</replaceable> is computed, then compared to
each of the <replaceable>value</replaceable> expressions in the
<token>WHEN</token> clauses until one is found that is equal to it. If
no match is found, the <replaceable>result</replaceable> of the
<token>ELSE</token> clause (or a null value) is returned. This is similar
to the <function>switch</function> statement in C.
最初のexpressionが計算され、そしてそれに等しいものが見つかるまでWHEN句のそれぞれのvalue式と比較されます。
等しいものが見つからない場合、ELSE句のresult(もしくはNULL値)が返されます。
これはC言語のswitch文に似ています。
The example above can be written using the simple
<token>CASE</token> syntax:
上の例は簡略形CASE構文を使って次のように書くことができます。
SELECT a,
CASE a WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'other'
END
FROM test;
a | case
---+-------
1 | one
2 | two
3 | other
A <token>CASE</token> expression does not evaluate any subexpressions
that are not needed to determine the result. For example, this is a
possible way of avoiding a division-by-zero failure:
CASE式は、結果を決定するために不必要などんな副式をも評価しません。
例えば、以下は0除算エラーを防ぐための方法です。
SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;
As described in <xref linkend="syntax-express-eval"/>, there are various
situations in which subexpressions of an expression are evaluated at
different times, so that the principle that <quote><token>CASE</token>
evaluates only necessary subexpressions</quote> is not ironclad. For
example a constant <literal>1/0</literal> subexpression will usually result in
a division-by-zero failure at planning time, even if it's within
a <token>CASE</token> arm that would never be entered at run time.
4.2.14で説明したとおり、式の副式が異なる時点で評価される様々な状況があります。そのため「CASEは必要な副式のみを評価する」という原則は厳格なものではありません。
例えば、定数1/0副式は、実行時には決して入らないCASE節の中にあったとしても、通常は計画時にゼロによる除算での失敗という結果に終わります。
COALESCE #COALESCE(value[, ...])
The <function>COALESCE</function> function returns the first of its
arguments that is not null. Null is returned only if all arguments
are null. It is often used to substitute a default value for
null values when data is retrieved for display, for example:
COALESCE関数は、NULLでない自身の最初の引数を返します。
全ての引数がNULLの場合にのみNULLが返されます。データを表示目的で取り出す際、NULL値をデフォルト値で置き換えるためによく使用されています。以下に例を示します。
SELECT COALESCE(description, short_description, '(none)') ...
This returns <varname>description</varname> if it is not null, otherwise
<varname>short_description</varname> if it is not null, otherwise <literal>(none)</literal>.
これはdescriptionがNULLでなければそれを返します。
そうでない場合(NULLの場合)は、short_descriptionがNULLでなければそれを返します。
それ以外の場合(short_descriptionもNULLの場合)は(none)が返ります。
The arguments must all be convertible to a common data type, which will be the type of the result (see <xref linkend="typeconv-union-case"/> for details). 引数はすべて共通の型に変換できる必要があり、それが結果の型になります。(詳細は10.5を参照してください。)
Like a <token>CASE</token> expression, <function>COALESCE</function> only
evaluates the arguments that are needed to determine the result;
that is, arguments to the right of the first non-null argument are
not evaluated. This SQL-standard function provides capabilities similar
to <function>NVL</function> and <function>IFNULL</function>, which are used in some other
database systems.
CASE式同様、COALESCEは結果を決定するために必要な引数のみを評価します。つまり、非NULL引数が見つかれば、その右側にある引数は評価されません。
この標準SQL関数は、他のいくつかのデータベースで使用されているNVLおよびIFNULLと類似の機能を提供します。
NULLIF #NULLIF(value1,value2)
The <function>NULLIF</function> function returns a null value if
<replaceable>value1</replaceable> equals <replaceable>value2</replaceable>;
otherwise it returns <replaceable>value1</replaceable>.
This can be used to perform the inverse operation of the
<function>COALESCE</function> example given above:
NULLIF関数は、value1がvalue2と等しい場合、NULL値を返します。
その他の場合はvalue1を返します。
これを使って、上記のCOALESCEの例の逆演算を実行できます
SELECT NULLIF(value, '(none)') ...
In this example, if <literal>value</literal> is <literal>(none)</literal>,
null is returned, otherwise the value of <literal>value</literal>
is returned.
この例では、valueが(none)ならばNULLが返ります。
さもなくばvalueを返します
The two arguments must be of comparable types.
To be specific, they are compared exactly as if you had
written <literal><replaceable>value1</replaceable>
= <replaceable>value2</replaceable></literal>, so there must be a
suitable <literal>=</literal> operator available.
2つの引数は比較可能な型でなければなりません。
具体的には、あたかもと書いたように比較されるので、適当なvalue1 = value2=演算子が使用できなければなりません。
The result has the same type as the first argument — but there is
a subtlety. What is actually returned is the first argument of the
implied <literal>=</literal> operator, and in some cases that will have
been promoted to match the second argument's type. For
example, <literal>NULLIF(1, 2.2)</literal> yields <type>numeric</type>,
because there is no <type>integer</type> <literal>=</literal>
<type>numeric</type> operator,
only <type>numeric</type> <literal>=</literal> <type>numeric</type>.
結果は最初の引数と同じ型ですが、微妙な場合があります。
実際に返却されるのは=演算子が暗示する最初の引数で、場合によっては2番目の引数にマッチするように昇格されています。
たとえばNULLIF(1, 2.2)はnumericを出力します。なぜならinteger = numeric演算子はなく、numeric = numericがあるだけだからです。
GREATESTおよびLEAST #GREATEST(value[, ...])
LEAST(value[, ...])
The <function>GREATEST</function> and <function>LEAST</function> functions select the
largest or smallest value from a list of any number of expressions.
The expressions must all be convertible to a common data type, which
will be the type of the result
(see <xref linkend="typeconv-union-case"/> for details).
GREATESTとLEAST関数は任意の数の式のリストから最大値もしくは最小値を選択します。
評価される全ての式は共通の型に変換できる必要があり、それが結果の型になります(詳細は10.5を参照してください)。
NULL values in the argument list are ignored. The result will be NULL only if all the expressions evaluate to NULL. (This is a deviation from the SQL standard. According to the standard, the return value is NULL if any argument is NULL. Some other databases behave this way.) 引数リストの中のNULL値は無視されます。 全ての式がNULLと評価された場合に限って結果はNULLになります。 (これは標準SQLからの逸脱です。 標準によれば、結果値は、いずれかの引数がNULLの場合はNULLになります。 他の一部のデータベースでは、このように動作します。)