result - 任何有效的字段或表达式。每个 WHEN 子句都必须有一个匹配的 THEN 子句,该子句用于指定在该条件为 true 时要返回的结果。如果有多个 WHEN 子句,CASE 语句会返回第一个为 true 的子句的结果。
else_result(可选)- 任何有效的字段或表达式。ELSEelse_result 子句用于为 CASE 语句指定默认结果。如果所有 WHEN 子句均不为 true,则返回此子句。如果 CASE 语句没有 ELSE 子句,并且所有 WHEN 子句均不为 true,则 CASE 语句会返回 NULL。
CASE 的简单运作方式
简单的 CASE 语句包含以下元素:
CASE 关键字,后跟输入表达式。
WHEN:与 input_expression 进行比较的值:如果该值等于 input_expression,则此子句为 true。您可以在单个 CASE 语句中使用多个 WHEN 子句。
THEN:如果 WHEN 子句的条件为 true,则返回的结果。在 CASE 语句中,每个 WHEN 子句都必须对应一个 THEN 子句。
ELSE:可选。如果所有 WHEN 子句条件均不为 true,CASE 会返回 ELSE 子句中的值,如果未指定 ELSE 子句,则返回 NULL。
END 关键字。
CASE 会计算每个连续 WHEN 子句,并在条件为 true 的情况下返回第一个结果。系统不会对任何剩余的 WHEN 子句和 ELSE 结果进行求值。如果所有 WHEN 条件均为 false 或 NULL,CASE 会返回 ELSE 结果;如果没有 ELSE 子句,则返回 NULL。
示例
为您的高级客户提供量身定制的链接:
CASE Premium Status
WHEN "Platinum" THEN CONCAT(Site URL, "platinum_welcome.html")
WHEN "Gold" THEN CONCAT(Site URL, "gold_welcome.html")
WHEN "Silver" THEN CONCAT(Site URL, "silver_welcome.html")
ELSE CONCAT(Site URL, "welcome.html")
END
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-03。"],[],[],null,["CASE (simple)\n=============\n\nSimple `CASE` returns a result based on the value of a single input expression, or a default result if none of the comparison values match.\n| **Note:** There are two forms of the `CASE` statement: *searched* `CASE` and *simple* `CASE`. Searched `CASE` statements allow you to use more sophisticated logic, while simple `CASE` statements are simpler to construct.\n\nSee also: [IF](/looker/docs/studio/if).\n\nSample usage\n------------\n\nReplace payment codes with intuitive names: \n\n```\n CASE Payment Type\n WHEN \"CC\" THEN \"Credit Card\"\n WHEN \"DC\" THEN \"Debit Card\"\n WHEN \"GC\" THEN \"Gift Card\"\n WHEN \"CA\" THEN \"Cash\"\n ELSE \"Other\"\n END\n```\n\nSyntax\n------\n\n```\n CASE input_expression\n WHEN expression_to_match THEN result\n [WHEN expression_to_match THEN result]\n [...]\n [ELSE else_result]\n END\n```\n\n### Parameters\n\n- \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e - Any valid field or expression.\n- \\[\u003cvar translate=\"no\"\u003eexpression_to_match\u003c/var\u003e - Any valid field or expression. The `WHEN` clause compares \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e to \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e and returns true if the two are equal, or false if they aren't.\\]{#when-conditions}\n- \u003cvar translate=\"no\"\u003eresult \u003c/var\u003e - Any valid field or expression. Each `WHEN` clause must have a matching `THEN` clause, which specifies the results to return if that condition is true. If there are multiple `WHEN` clauses, the `CASE` statement returns the result for the first true clause.\n- \u003cvar translate=\"no\"\u003eelse_result\u003c/var\u003e (optional) - Any valid field or expression. The `ELSE` *else_result* clause specifies a default result for the `CASE` statement. This clause is returned if none of the `WHEN` clauses are true. If a `CASE` statement has no `ELSE` clause, and none of the `WHEN` clauses are true, the `CASE` statement returns `NULL`.\n\n| **Note:** All of the `THEN` clauses in a `CASE` statement must return the same type of result. For example, if the first `THEN` clause returns the **Text** data type, additional `THEN` clauses must also return the **Text** data type.\n\nHow simple `CASE` works\n-----------------------\n\nA simple `CASE` statement consists of the following elements:\n\n- The `CASE` keyword, followed by an input expression.\n- `WHEN` : the value against which to compare the \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e : if the value equals the \u003cvar translate=\"no\"\u003einput_expression\u003c/var\u003e, then this clause is true. You can have multiple `WHEN` clauses in a single `CASE` statement.\n- `THEN` : the result to return if the `WHEN` clause's condition is true. You must have one `THEN` clause for each `WHEN` clause in your `CASE` statement.\n- `ELSE` : Optional. If none of the `WHEN` clause conditions are true, `CASE` returns the value in the `ELSE` clause, or `NULL` if no `ELSE` clause is specified.\n- The `END` keyword.\n\n`CASE` evaluates each successive `WHEN` clause and returns the first result where the condition is true. Any remaining `WHEN` clauses and the `ELSE` result are not evaluated. If all `WHEN` conditions are false or `NULL`, `CASE` returns the `ELSE` result, or if no `ELSE` clause is present, returns `NULL`.\n\nExample\n-------\n\nProvide customized links for your premium customers: \n\n```\n CASE Premium Status\n WHEN \"Platinum\" THEN CONCAT(Site URL, \"platinum_welcome.html\")\n WHEN \"Gold\" THEN CONCAT(Site URL, \"gold_welcome.html\")\n WHEN \"Silver\" THEN CONCAT(Site URL, \"silver_welcome.html\")\n ELSE CONCAT(Site URL, \"welcome.html\")\n END\n```\n\nRelated resources\n-----------------\n\n- [Calculated fields](/looker/docs/studio/about-calculated-fields)\n- [Looker Studio function list](/looker/docs/studio/function-list)"]]