switch - Amazon QuickSight

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

switch

switchcondition-expression 與在一組常值標籤和 return-expression 對中的常值標籤進行比較。然後,它會傳回與等於 condition-expression 的第一個常值標籤相對應的 return-expression。如果沒有標籤等於 condition-expression,則 switch 傳回 default-expression。每個 return-expressiondefault-expression 必須具有相同的資料類型。

語法

switch(condition-expression, label-1, return-expression-1 [, label-n, return-expression-n ...], default-expression)

引數

switch 需要一或多個 ifthen 表達式配對,加上 else 引數的正好一個表達式。

condition-expression

要與標籤常值進行比較的表達式。它可以是 address 之類的欄位名稱、Unknown 之類的常值,或 toString(salesAmount) 之類的純量函數。

label

要與 condition-expression 引數進行比較的常值,所有常值必須具有與 condition-expression 引數相同的資料類型。switch 最多可接受 5,000 個標籤。

return-expression

標籤值等於 condition-expression 的值傳回的表達式。它可以是 address 之類的欄位名稱、Unknown 之類的常值,或 toString(salesAmount) 之類的純量函數。所有 return-expression 引數必須與 default-expression 具有相同的資料類型。

default-expression

沒有任何標籤引數的值等於 condition-expression 的值時傳回的表達式。它可以是 address 之類的欄位名稱、Unknown 之類的常值,或 toString(salesAmount) 之類的純量函數。所有 default-expression 必須與 default-expression 的所有引數具有相同的資料類型。

傳回類型

switch 會傳回與 return-expression 中的值具有相同資料類型的值。從 return-expressiondefault-expression 表達式傳回的所有資料必須具有相同的資料類型或轉換為相同的資料類型。

一般範例

下列範例會傳回輸入區域名稱的 AWS 區域 程式碼。

switch(region_name, "US East (N. Virginia)", "us-east-1", "Europe (Ireland)", "eu-west-1", "US West (N. California)", "us-west-1", "other regions")

以下是指定欄位的值。

"US East (N. Virginia)" "US West (N. California)" "Asia Pacific (Tokyo)"

對於這些欄位值,會傳回以下值。

"us-east-1" "us-west-1" "other regions"

使用 switch 取代 ifelse

以下 ifelse 使用案例與前面的範例等效,相較於使用 ifelse 評估一個欄位的值是否等於不同的常值,使用 switch 是更好的選擇。

ifelse(region_name = "US East (N. Virginia)", "us-east-1", region_name = "Europe (Ireland)", "eu-west-1", region_name = "US West (N. California)", "us-west-1", "other regions")

表達式作為傳回值

以下範例在 return-expressions 中使用表達式:

switch({origin_city_name}, "Albany, NY", {arr_delay} + 20, "Alexandria, LA", {arr_delay} - 10, "New York, NY", {arr_delay} * 2, {arr_delay})

前面的範例變更了來自特定城市的每個航班的預期延誤時間。

函數範例結果的影像 (以資料表形式顯示)。