建議在進行更複雜的區隔作業,或實作更精細的 ELSE 陳述式時,使用 SQL CASE WHEN 陳述式。
舉例來說,您可能會根據訂單的目的地使用不同的分組方法。您可以使用 SQL CASE WHEN 陳述式建立複合值區維度,其中 THEN 陳述式會傳回維度,而非字串:
dimension: compound_buckets {
sql:
CASE
WHEN ${orders.destination} = 'US' THEN ${us_buckets}
WHEN ${orders.destination} = 'CA' THEN ${canada_buckets}
ELSE ${intl_buckets}
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"]],["上次更新時間:2025-07-31 (世界標準時間)。"],[],[],null,["# Bucketing in Looker\n\n\u003e *When you have [permission to create custom fields](/looker/docs/admin-panel-users-roles#create_custom_fields), you can create ad hoc [custom groups](/looker/docs/custom-fields#custom_grouping) for dimensions without using [logical functions](/looker/docs/functions-and-operators#logical_functions,_operators,_and_constants) in [Looker expressions](/looker/docs/creating-looker-expressions) or developing `CASE WHEN` logic in [`sql`](/looker/docs/reference/param-field-sql) parameters or [`type: case`](/looker/docs/reference/param-field-case) fields.*\n\u003e *You can also create ad hoc [custom bins](/looker/docs/custom-fields#custom_binning) for numeric type dimensions without needing to use [logical functions](/looker/docs/functions-and-operators#logical_functions,_operators,_and_constants) in [Looker expressions](/looker/docs/creating-looker-expressions) or needing to develop [`type: tier`](/looker/docs/reference/param-dimension-filter-parameter-types#tier) LookML fields when you have [permission to create custom fields](/looker/docs/admin-panel-users-roles#create_custom_fields).*\n\n\nBucketing can be very useful for creating custom grouping dimensions in Looker.\n\n\nThere are three ways to create buckets in Looker:\n\n1. Using the [`tier`](/looker/docs/reference/param-dimension-filter-parameter-types#tier) `dimension` type\n2. Using the [`case`](/looker/docs/reference/param-field-case) parameter\n3. Using a SQL `CASE WHEN` statement in the [`SQL`](/looker/docs/reference/param-field-sql) parameter of a LookML field\n\n\u003cbr /\u003e\n\nUsing `tier` for bucketing\n--------------------------\n\n\nTo create integer buckets, we can simply define the `dimension` type as [`tier`](http://www.looker.com/docs/reference/field-reference/dimension-type-reference#tier): \n\n```\ndimension: users_lifetime_orders_tier {\n type: tier\n tiers: [0,1,2,5,10]\n sql: ${users_lifetime_orders} ;;\n}\n```\n\n\nYou can use the [`style`](/looker/docs/reference/param-dimension-filter-parameter-types#style) parameter to customize how your tiers appear when exploring. The four options for `style` are as follows:\n\n1. [`classic`](/looker/docs/reference/param-dimension-filter-parameter-types#classic)\n2. [`integer`](/looker/docs/reference/param-dimension-filter-parameter-types#integer)\n3. [`interval`](/looker/docs/reference/param-dimension-filter-parameter-types#interval)\n4. [`relational`](/looker/docs/reference/param-dimension-filter-parameter-types#relational)\n\n\u003cbr /\u003e\n\n\nFor example: \n\n```\ndimension: age_tier {\n type: tier\n tiers: [0,10,20,30,40,50,60,70,80]\n style: integer\n sql: ${age} ;;\n}\n```\n\n\nThe `style` parameter `classic` is the default and takes the format `Tx[x,x]` with `Tx` indicating the tier number and `[x,x]` indicating the range. The following image is an Explore data table with **Users Count** grouped by **Users Age**:\n\n\nThe next image shows examples of the other `style` parameter options:\n\n- `interval` --- With the format `[x,x]`, which indicates the lowest value and the highest value of a tier\n- `integer` --- With the format `x to x`, which indicates the lowest value and the highest value of a tier\n- `relational` --- With the format `\u003e= x and \u003cx`, which indicates that a value is greater than or equal to the lowest tier value and less than the highest tier value\n\n\u003cbr /\u003e\n\n\n### Things to consider\n\n\nUsing `tier` in conjunction with [dimension fill](/looker/docs/creating-visualizations#filling_in_missing_dates_and_values) can result in unexpected tier buckets.\n\n\nFor example, a `type: tier` dimension, **Age Tier** , will display tier buckets for **Below 0** and **0 to 9** when dimension fill is enabled, although the data does not include age values for those buckets:\n\n\nWhen dimension fill is disabled for **Age Tier** , the buckets more accurately reflect the age values available in the data, beginning with the bucket **10 to 19**:\n\n\nYou can enable or disable dimension fill by hovering over the dimension name in the Explore, clicking the field-level [gear icon](/looker/docs/creating-visualizations#to_use_dimension_fill), and selecting either **Remove Filled in Tier Values** to disable, or **Fill in Missing Tier Values** to enable.\n\n\nFind out more about Looker [`tiers`](http://www.looker.com/docs/reference/lookml-reference/dimension-lookml-reference#tier) on the **Dimension, filter, and parameter types** documentation page.\n\nUsing `case` for bucketing\n--------------------------\n\n\nYou can use the [`case`](http://www.looker.com/docs/reference/field-reference#case) parameter to create custom-named buckets with custom sorting. The `case` parameter is recommended for a fixed set of buckets, as it can help control the way values are presented, ordered, and used in UI filters and visualizations. For example, with `case`, a user will be able to select only the defined bucket values in a filter.\n\n\nTo create buckets with `case`, you can define a dimension, like a bucket for order amounts: \n\n```\n dimension: order_amount_bucket {\n case: {\n when: {\n sql: ${order_amount} \u003c= 50;;\n label: \"Small\"\n }\n when: {\n sql: ${order_amount} \u003e 50 AND ${order_amount} \u003c= 150;;\n label: \"Medium\"\n }\n when: {\n sql: ${order_amount} \u003e 150;;\n label: \"Large\"\n }\n else:\"Unknown\"\n }\n }\n```\n\n\nThe `case` parameter will typically sort values in the order in which the buckets are listed. For the `order_amount_bucket` dimension, the order of the buckets is **Small** , **Medium** , and **Large**:\n\n\nIf you would like to sort alphanumerically, add the [`alpha_sort`](/looker/docs/reference/param-field-case#choosing_the_sort_order_of_labels_with_alpha_sort) parameter\nto the dimension, like so: \n\n```\n dimension: order_amount_bucket {\n alpha_sort: yes\n case: {\n when: {\n sql: ${order_amount} \u003c= 50;;\n label: \"Small\"\n }\n when: {\n sql: ${order_amount} \u003e 50 AND ${order_amount} \u003c= 150;;\n label: \"Medium\"\n }\n when: {\n sql: ${order_amount} \u003e 150;;\n label: \"Large\"\n }\n else:\"Unknown\"\n }\n }\n```\n\n\nFor dimensions where many distinct values are desired in the output (this would require you to define each output with a `WHEN` or an `ELSE` statement), or when you would like to implement a more complex `ELSE` statement, we recommend that you use a SQL `CASE WHEN`, discussed in the next section.\n\n\nRead more about the [`case`](/looker/docs/reference/param-field#sql_case) parameter on the **Field parameters** documentation page.\n\nUsing SQL `CASE WHEN` for bucketing\n-----------------------------------\n\n\nA SQL `CASE WHEN` statement is recommended for more complex bucketing, or for implementation of a more nuanced `ELSE` statement.\n\n\nFor example, you may want to use different bucketing methods, depending on the destination of an order. A SQL `CASE WHEN` statement could be used to create a compound bucket dimension, where the `THEN` statement returns dimensions rather than strings: \n\n```\ndimension: compound_buckets {\n sql:\n CASE\n WHEN ${orders.destination} = 'US' THEN ${us_buckets}\n WHEN ${orders.destination} = 'CA' THEN ${canada_buckets}\n ELSE ${intl_buckets}\n END ;;\n}\n```"]]