Składnia SQL Case Expression?
Jaka jest kompletna i poprawna składnia wyrażenia przypadku SQL?
8 answers
Składnia kompletna zależy od silnika bazy danych, z którym pracujesz:
Dla SQL Server:
CASE case-expression
WHEN when-expression-1 THEN value-1
[ WHEN when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
Lub:
CASE
WHEN boolean-when-expression-1 THEN value-1
[ WHEN boolean-when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
Wyrażenia itp.:
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
Link: CASE (Transact-SQL)
Należy również pamiętać, że kolejność wyrażeń WHEN jest ważna. Można łatwo zapisać wiele klauzul, które nakładają się na siebie, i pierwszy, który pasuje jest używany .
Notatka: Jeśli nie podano klauzuli ELSE I nie ma dopasowania Gdy zostanie znaleziony warunek, wartość wyrażenia CASE wyniesie NULL.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-07 14:26:21
Biorąc pod uwagę, że otagowałeś wiele produktów, powiedziałbym, że Pełna poprawna składnia byłaby taka, jaką można znaleźć w standardzie 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>
Zasady Składni
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>.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-07 14:29:03
Oto CASE
Przykłady instrukcji z dokumentów PostgreSQL (Postgres stosuje tutaj standard SQL):
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;
Lub
SELECT a,
CASE a WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'other'
END
FROM test;
Oczywiście drugi formularz jest czystszy, gdy tylko sprawdzasz jedno pole względem listy możliwych wartości. Pierwsza forma pozwala na bardziej skomplikowane wyrażenia.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-17 14:12:22
Sybase ma taką samą składnię case Jak SQL Server:
Opis
Obsługuje warunkowe wyrażenia SQL; może być używany wszędzie tam, gdzie można użyć wyrażenia wartości.
Składnia
case
when search_condition then expression
[when search_condition then expression]...
[else expression]
end
Składnia Case i wartości
case expression
when expression then expression
[when expression then expression]...
[else expression]
end
Parametry
Case
Rozpoczyna wyrażenie case.
Kiedy
Poprzedza warunek wyszukiwania lub wyrażenie, które ma być porównane.
Search_condition
Służy do ustawiania warunków dla wybranych wyników. Warunki wyszukiwania dla wyrażeń case są podobne do warunków wyszukiwania w klauzuli where. Warunki wyszukiwania są szczegółowo opisane w Transact - SQL User ' s Guide.
Then
Poprzedza wyrażenie, które określa wartość wyniku case.
Wyrażenie
Jest nazwą kolumny, stałą, funkcją, zapytaniem podrzędnym lub dowolną kombinacją nazw kolumn, stałych i funkcji połączonych operatorami arytmetycznymi lub bitowymi. Więcej informacje o wyrażeniach, patrz "wyrażenia" w.
Przykład
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-26 19:05:29
Wykopałem stronę Oracle dla tego samego i wygląda na to, że to ta sama składnia, tylko opisana nieco inaczej.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-27 12:59:50
Oracle składnia z Dokumentacji 11g :
CASE { simple_case_expression | searched_case_expression }
[ else_clause ]
END
Simple_case_expression
expr { WHEN comparison_expr THEN return_expr }...
Searched_case_expression
{ WHEN condition THEN return_expr }...
Else_clause
ELSE else_expr
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-10 15:26:31
Należy zwrócić uwagę na jeden punkt w przypadku Oracle, jeśli nie, gdy pasuje i nie ma innej części, zostanie podniesiony wyjątek.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-02-01 07:19:45
Składnia instrukcji Case w SQL SERVER:
CASE column
WHEN value1 THEN 1
WHEN value3 THEN 2
WHEN value3 THEN 3
WHEN value1 THEN 4
ELSE ''
END
I możemy użyć jak poniżej również:
CASE
WHEN column=value1 THEN 1
WHEN column=value3 THEN 2
WHEN column=value3 THEN 3
WHEN column=value1 THEN 4
ELSE ''
END
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-24 17:47:31