result - 任意类型的字段或表达式。每个 WHEN 子句都必须有一个匹配的 THEN 子句,用于指定 WHEN condition 为 true 时的结果。如果有多个 WHEN 子句,CASE 语句会针对第一个为 true 的 condition 返回 result。
else_result(可选)- 任意类型的字段或表达式。ELSEelse_result 子句用于为 CASE 语句指定默认结果。如果所有 WHEN 子句均不为 true,则返回此子句。如果 CASE 语句没有 ELSE 子句,并且所有 WHEN 子句均不为 true,则 CASE 语句会返回 NULL。
一个 CASE 语句只能有一个 ELSE 子句。
搜索到的 CASE 的运作方式
搜索到的 CASE 语句以 CASE 关键字开头,以 END 关键字结尾。中间会包含多个部分或条款:
WHEN:您要评估的条件。您可以在单个 CASE 语句中使用多个 WHEN 子句。
THEN:如果 WHEN 子句的条件为 true,则返回的结果。在 CASE 语句中,每个 WHEN 子句都必须对应一个 THEN 子句。
ELSE:可选。如果所有 WHEN 子句条件均不为 true,CASE 会返回 ELSE 子句中的值,如果未指定 ELSE 子句,则返回 NULL。
CASE 会计算每个连续 WHEN 子句,并在条件为 true 的情况下返回第一个结果。系统不会对任何剩余的 WHEN 子句和 ELSE 结果进行求值。如果所有 WHEN 条件均为 false 或 NULL,CASE 会返回 ELSE 结果;如果没有 ELSE 子句,则返回 NULL。
示例
检查是否不等
CASE WHEN Medium != "cpc" THEN "free" ELSE "paid" END
将数值维度值分类到离散存储分区中
例如,您可以根据订单金额将订单划分为“小额”“中额”或“大额”:
CASE
WHEN Amount < 20 THEN "Small"
WHEN Amount >= 20 and Amount < 100 THEN "Medium"
WHEN Amount >= 100 THEN "Large"
END
将日期维度值分类到离散分桶
例如,您可以根据特定日期将一年分为两个学期:
CASE
WHEN Date >= DATE(2018, 9, 23) and Date < DATE(2018, 12, 13) THEN "Fall Semester 2018"
WHEN Date >= DATE(2018, 1, 3) and Date < DATE(2019, 3, 21) THEN "Winter Semester 2019"
WHEN Date >= DATE(2019, 3, 27) and Date < DATE(2019, 6, 12) THEN "Spring Semester 2019"
ELSE "Uncategorized"
END
评估逻辑 AND 条件
CASE
WHEN Country ISO Code = "US" AND Medium = "cpc"
THEN "US - Paid"
ELSE "other"
END
评估逻辑 AND/OR 条件
CASE
WHEN REGEXP_MATCH(Video Title, ".*Google Analytics*")
AND is_livestream = TRUE
OR Video Length > 600
THEN "GA Livestream or long video"
END
根据参数值返回不同的字段或值
示例 1:根据所选的参数值返回维度。您可以使用此方法让用户切换图表使用的细分维度。
CASE
WHEN Breakdown = "Product Category" THEN Product Category
WHEN Breakdown = "Store" THEN Store
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 (Searched)\n===============\n\n`CASE` statements evaluate one or more conditions and return a result when the first condition is met, or a default result if none of the conditions are met.\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\nA common use for `CASE` is to create new categories or groupings of data. For example, to group selected country values into a Sales Region dimension, you might create a `CASE` statement like this: \n\n```\n\n CASE\n WHEN Country IN (\"USA\",\"Canada\",\"Mexico\") THEN \"North America\"\n WHEN Country IN (\"England\",\"France\") THEN \"Europe\"\n ELSE \"Other\"\n END\n\n```\n\nSyntax\n------\n\n```\n\n CASE\n WHEN condition THEN result\n [WHEN condition THEN result]\n [...]\n [ELSE else_result]\n END\n\n```\n\n### Parameters\n\n- \u003cvar translate=\"no\"\u003econdition\u003c/var\u003e - A logical field or expression. `WHEN` clauses evaluate \u003cvar translate=\"no\"\u003econdition\u003c/var\u003e and return true if the specified condition is met, or false if the condition isn't met.\n\n- \u003cvar translate=\"no\"\u003eresult\u003c/var\u003e - A field or expression of any type. Each `WHEN` clause must have a matching `THEN` clause, which specifies the results if that `WHEN `\u003cvar translate=\"no\"\u003econdition\u003c/var\u003e is true. If there are multiple `WHEN` clauses, the `CASE` statement returns the \u003cvar translate=\"no\"\u003eresult\u003c/var\u003e for the first true \u003cvar translate=\"no\"\u003econdition\u003c/var\u003e.\n\n- \u003cvar translate=\"no\"\u003eelse_result\u003c/var\u003e (optional) - A field or expression of any type. The `ELSE` \u003cvar translate=\"no\"\u003eelse_result \u003c/var\u003e 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\nA `CASE` statement can have only one `ELSE` clause.\n| **Note:** All of the `THEN` clauses in a `CASE` statement must return the same data type. For example, if the first `THEN` clause returns the **Text** data type, additional `THEN` clauses must also return the **Text** data type.\n\nHow searched `CASE` works\n-------------------------\n\nA searched `CASE` statement begins with the `CASE` keyword and ends with the `END` keyword. In between, you'll have a number of sections or clauses:\n\n- `WHEN`: A condition that you want to evaluate. 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\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\nExamples\n--------\n\n### Check for inequality\n\n```\n CASE WHEN Medium != \"cpc\" THEN \"free\" ELSE \"paid\" END\n```\n| **Tip:** For simple scenarios, consider using the [IF function](/looker/docs/studio/if).\n\n### Classify numeric dimension values into discrete buckets\n\nFor example, you can separate orders into \"Small\", \"Medium\", or \"Large\" based on order amount: \n\n```\n CASE\n WHEN Amount \u003c 20 THEN \"Small\"\n WHEN Amount \u003e= 20 and Amount \u003c 100 THEN \"Medium\"\n WHEN Amount \u003e= 100 THEN \"Large\"\n END\n```\n| **Note:** You can also use a [Group calculated field](/looker/docs/studio/create-a-custom-group) to create ad hoc groups for dimension values.\n\n### Classify date dimension values into discrete buckets\n\nFor example, you can separate a year into semesters based on specific dates: \n\n```\n CASE\n WHEN Date \u003e= DATE(2018, 9, 23) and Date \u003c DATE(2018, 12, 13) THEN \"Fall Semester 2018\"\n WHEN Date \u003e= DATE(2018, 1, 3) and Date \u003c DATE(2019, 3, 21) THEN \"Winter Semester 2019\"\n WHEN Date \u003e= DATE(2019, 3, 27) and Date \u003c DATE(2019, 6, 12) THEN \"Spring Semester 2019\"\n ELSE \"Uncategorized\"\n END\n```\n\n### Evaluate a logical AND condition\n\n```\n CASE\n WHEN Country ISO Code = \"US\" AND Medium = \"cpc\"\n THEN \"US - Paid\"\n ELSE \"other\"\n END\n```\n\n### Evaluate a logical AND/OR condition\n\n```\n CASE\n WHEN REGEXP_MATCH(Video Title, \".*Google Analytics*\")\n AND is_livestream = TRUE\n OR Video Length \u003e 600\n THEN \"GA Livestream or long video\"\n END\n```\n\n### Return a different field or value depending on a parameter value\n\n**Example 1**: Return a dimension based on the parameter value selected. You can use this technique to let users switch the breakdown dimension used by a chart. \n\n```\n CASE\n WHEN Breakdown = \"Product Category\" THEN Product Category\n WHEN Breakdown = \"Store\" THEN Store\n END\n```\n\nYou could also write this using [simple CASE syntax](/looker/docs/studio/case-simple): \n\n```\n CASE Breakdown\n WHEN \"Product Category\" THEN Product Category\n WHEN \"Store\" THEN Store\n END\n```\n\n**Example 2**: Return a different metric based on a parameter value. \n\n```\nCASE WHEN Show Adjusted Cost = TRUE THEN Cost + Adjustments ELSE Cost END\n```\n\n### Nested `CASE` statements\n\nYou can nest `CASE` statements to create more complex branching logic: \n\n```\n CASE WHEN REGEXP_CONTAINS(SKU, \"Hats\") THEN\n CASE WHEN Color = \"blue\"\n THEN \"BLUE HAT\" ELSE \"JUST A HAT\"\n END\n ELSE \"NOT A HAT\"\n END\n```\n\nRelated resources\n-----------------\n\n- [CASE (simple)](/looker/docs/studio/case-simple)\n- [Calculated fields](/looker/docs/studio/about-calculated-fields)\n- [Looker Studio function list](/looker/docs/studio/function-list)"]]