客服案件

用量

view: view_name {
  dimension: field_name {
    case: {
      when: {
        sql: SQL condition ;;
        label: "value"
      }
      # Possibly more when statements
      else: "value"
    }
    alpha_sort:  yes
  }
}
階層
case
可能的欄位類型
維度

接受
SQL 條件和字串

特別規則
如要依字母順序排列值,請使用 alpha_sort 參數

定義

case 可讓您使用案例邏輯將結果分類。雖然您可以改為編寫原始 SQL CASE 陳述式,但使用 case 會在 Looker UI 中為使用者建立下拉式選單。SQL CASE 陳述式不會建立這類選單。

case 的一般形式如下:

dimension: status {
  case: {
    when: {
      sql: condition ;;
      label: "Label of Condition"
    }
    # possibly more when statements
    else: "Label If No Condition Met"
  }
}

這些參數的運作方式如下:

  • when:您可以視需要使用多個 when 陳述式,代表要提供標籤的每個條件。系統會依列出的順序評估 when 陳述式,並指派第一個評估結果為 true 的 when 陳述式相關聯的標籤。
  • sqlsql 參數接受評估結果為 True 或 False 的 SQL 條件。
  • label:如果 SQL 條件為 True,系統會指派這個標籤。指派的標籤資料類型為 stringcase 陳述式中每個 label 的值不得重複。如果多個 SQL 條件使用相同的 label 值,則只有 case 陳述式中的最後一個 SQL 條件會指派 label 值。請參閱本頁的「範例」。
  • else:如果沒有任何條件符合,系統就會使用這個標籤。

使用 alpha_sort 選擇標籤的排序順序

通常 case 值會按照您撰寫的順序顯示。如要依字母順序排序,可以使用 alpha_sort: yes,如下所示:

dimension: status {
  alpha_sort: yes
  case: { ... }
}

範例

為不同狀態編號指派多個可讀取的標籤:

dimension: status {
  case: {
    when: {
      sql: ${TABLE}.status = 0 ;;
      label: "pending"
    }
    when: {
      sql: ${TABLE}.status = 1 ;;
      label: "complete"
    }
    when: {
      sql: ${TABLE}.status = 2 ;;
      label: "returned"
    }
    else: "unknown"
  }
}

如果重複使用相同條件,但評估結果為不同標籤,LookML 會使用評估結果為 true 的第一個條件。在下列範例中,${TABLE}.status = 0 會評估為 pending,而非 returned,因為系統會先評估 pending 條件。

dimension: status {
  case: {
    when: {
      sql: ${TABLE}.status = 0 ;;
      label: "pending"
    }
    when: {
      sql: ${TABLE}.status = 1 ;;
      label: "complete"
    }
    when: {
      sql: ${TABLE}.status = 0 ;;
      label: "returned"
    }
    else: "unknown"
  }
}

如果多個條件評估結果為相同標籤,LookML 只會使用第一個條件。在下列範例中,Looker 會使用 ${TABLE}.status = 0 (而非 ${TABLE}.status = 2) 生成 SQL CASE 陳述式,評估結果為 pending。如果為 ${TABLE}.status = 2CASE 陳述式會評估為 unknown

view: orders
dimension: status {
  case: {
    when: {
      sql: ${TABLE}.status = 0 ;;
      label: "pending"
    }
    when: {
      sql: ${TABLE}.status = 1 ;;
      label: "complete"
    }
    when: {
      sql: ${TABLE}.status = 2 ;;
      label: "pending"
    }
    else: "unknown"
  }
}

注意事項

如果維度包含參照其他欄位的 case 參數,系統可能會將該額外欄位新增至維度所用查詢的基礎 SQL。如果查詢的視覺化效果中沒有參照欄位,且視覺化效果是手動重新排列資料欄的表格圖表,部分下載格式的資料欄順序可能會受到影響。