SQL 케이스 표현식 구문?
-
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
또한 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;
분명히 두 번째 형식은 가능한 값 목록에 대해 하나의 필드를 확인할 때 더 깔끔합니다.첫 번째 형식은 더 복잡한 표현을 허용합니다.
Sybase도 마찬가지입니다 대소문자 구문 SQL Server로:
설명
조건부 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 표현식의 검색 조건은 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 페이지를 파헤쳤는데 이것이 동일한 구문인 것처럼 보이지만 약간 다르게 설명되었습니다.
링크: 오라클/PLSQL:사례 진술
신탁 11g 문서의 구문:
CASE { simple_case_expression | searched_case_expression }
[ else_clause ]
END
단순_케이스_표현
expr { WHEN comparison_expr THEN return_expr }...
검색된_케이스_표현
{ WHEN condition THEN return_expr }...
else_clause
ELSE else_expr
Oracle의 경우 주의할 점은 일치하는 부분이 없고 다른 부분이 없으면 예외가 발생한다는 것입니다.
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