explore_source

本頁面是指 explore_source 參數,這是 derived_table 的子參數。

explore_source 也可以是 test 的子參數,詳情請參閱 test 參數說明文件頁面。

用量

derived_table: customer_order_facts {
  explore_source:  orders {
    column:  customer_id {
      field:  orders.customer_id
    }
    column:  order_amount {
      field:  orders.sale_price
    }
    column:  item_qty {
      field:  orders.number_items
    }
    derived_column:  average_item_price {
      sql:  order_amount / item_qty  ;;
    }
    timezone:  "America/Los_Angeles"
  }
}
階層
explore_source
預設值

接受
原生衍生資料表所衍生自的探索 ID,以及定義原生衍生資料表的子參數。

定義

您可以透過兩種方式建立衍生資料表,並在資料庫中像使用一般資料表一樣使用衍生資料表:原生衍生資料表 (使用 LookML 參數定義) 和以 SQL 為基礎的衍生資料表 (使用 SQL 查詢陳述式定義)。

explore_source 參數用於原生衍生資料表。您可以在這個參數中定義要納入原生衍生資料表的資料欄、要套用至原生衍生資料表的任何篩選器、是否要限制或排序原生衍生資料表資料列,以及是否要將原生衍生資料表的時基欄位轉換為其他時區。

定義原生衍生資料表

您可以在原生衍生表格中使用各種參數,其中許多是選用參數。定義原生衍生資料表的語法如下:

explore_source: identifier {
  bind_all_filters: yes
  column: identifier {
    field: field_name
  }
  derived_column: identifier {
    sql: SQL expression ;;
  }
  expression_custom_filter: [custom filter expression]
  filters: [field_name_1: "string", field_name_2: "string", ...]
  limit: number
  sorts: [field_name_1: asc | desc, field_name_2: asc | desc, ...]
  timezone: "string"
}

下表提供各項參數的相關資訊,可用於定義原生衍生資料表:

參數名稱 說明 範例
bind_filters 將探索查詢中的篩選器傳遞至原生衍生資料表子查詢。如要設定這個參數,請使用 from_field 子參數,指定原生衍生資料表檢視畫面中定義的欄位,或可存取原生衍生資料表所加入「探索」的欄位。在執行階段,探索中的 from_field 上的任何篩選器都會傳遞至原生衍生資料表子查詢中的 to_field。如需範例,請參閱「使用 bind_filters」一節。

使用 bind_filters 時,請注意以下事項:
  • 執行階段篩選條件必須位於與原生衍生資料表加入相同探索的檢視區塊中。
  • 原生衍生資料表無法做為永久衍生資料表 (PDT)

explore_source 參數可以有 bind_all_filters 子參數 bind_filters 子參數,但不能同時有兩者。
bind_filters: {
  to_field: users.created_date
  from_field: user_dt.filter_date
}
bind_all_filters 將探索查詢中的所有篩選器傳遞至原生衍生資料表子查詢。如要設定這個參數,請在原生衍生資料表的 explore_source 中指定 bind_all_filters: yes。如需範例,請參閱「使用 bind_filters」一節。

使用 bind_all_filters: yes 時,請注意以下事項:
  • 執行階段篩選條件必須位於與原生衍生資料表加入相同探索的檢視區塊中。
  • 原生衍生資料表無法轉換為 PDT
  • 原生衍生資料表只能加入原生衍生資料表的 explore_source 參數中指定的「探索」。這是因為 bind_all_filters 需要將探索的篩選欄位對應至原生衍生資料表中的欄位。
  • explore_source 參數可以有 bind_all_filters 子參數 bind_filters 子參數,但不能同時有這兩者。
bind_all_filters: yes
column 指定要納入 explore_source 的資料欄。具有 field 子參數。
column: cust_id {
  field: orders.customer_id
}
derived_column explore_source 中指定資料欄,並在內部資料欄的命名空間中加入運算式。由於這個步驟沒有 SQL 分組,因此這裡無法使用匯總 SQL 運算式。SQL 窗型函式在這個參數中非常實用。具有 sql 子參數。
derived_column: average_order {
  sql: order_amount / item_qty ;;
}
dev_filters 已於 21.12 新增 指定 Looker 僅套用至衍生資料表開發版本的篩選器。LookML 開發人員在開發模式中測試衍生資料表時,這項功能非常實用。Looker 可透過 dev_filters 參數建構較小的資料表篩選版本,讓 LookML 開發人員疊代及測試資料表,而不必在每次變更後等待完整資料表建構完成。Looker 只會將 dev_filters 套用至衍生資料表的開發版本,不會套用至使用者查詢的資料表正式版。如要進一步瞭解如何在開發模式中使用衍生資料表,請參閱「Looker 中的衍生資料表」說明文件頁面,並參閱本頁的「為開發模式建立篩選器」一節中的範例。 dev_filters: [orders.created_date: "90 days", orders.products: "sweaters"]
expression_custom_filter (選用) 指定 explore_source 查詢的自訂篩選運算式 expression_custom_filter: ${orders.status} = "pending" ;;
filters 視需要在 explore_source 查詢中加入篩選器。以方括號括住;加入要篩選的欄位名稱(使用 view_name.field_name 格式),然後加上 : 和要篩選欄位的值。篩選器會新增至原生衍生資料表產生的 SQL WHERE 子句。 filters: [products.department: "sweaters"]
limit (選用) 指定查詢的資料列限制。 limit: 10
sorts (選用) 指定這個 explore_source 的排序方式。以方括號括住;使用 view_name.field_name 格式加入要排序的欄位名稱,然後加上 :ascdesc,指出欄位應以遞增或遞減排序。如要依多個欄位排序,請新增多個以半形逗號分隔的欄位名稱和關鍵字配對。 sorts: [products.brand: asc, products.name: asc]
timezone 設定 explore_source 查詢的時區。對於非持續性衍生資料表,請將時區設為 "query_timezone",自動使用目前執行查詢的時區。如果未指定時區,explore_source 查詢不會執行任何時區轉換,而是使用資料庫時區。如需支援的時區清單,請參閱timezone 值頁面

在 IDE 中輸入 timezone 參數時,IDE 會自動建議時區值。IDE 也會在「快速說明」面板中顯示支援的時區值清單。
timezone: "America/Los_Angeles"

範例

以下定義提供原生衍生資料表的基本範例。

建立user_order_facts原生衍生資料表:

view: user_order_facts {
  derived_table: {
    explore_source: order_items {
      column: user_id {
        field: order_items.user_id
      }
      column: lifetime_number_of_orders {
        field: order_items.order_count
      }
      column: lifetime_customer_value {
        field: order_items.total_revenue
      }
    }
  }
  # Define the view's fields as desired
  dimension: user_id {
    hidden: yes
  }
  dimension: lifetime_number_of_orders {
    type: number
  }
  dimension: lifetime_customer_value {
    type: number
  }
}

您可以新增篩選器,建立 user_90_day_facts 原生衍生資料表:

view: user_90_day_facts {
  derived_table: {
    explore_source: order_items {
      column: user_id {
        field: order_items.user_id
      }
      column: number_of_orders_90_day {
        field: order_items.order_count
      }
      column: customer_value_90_day {
        field: order_items.total_revenue
      }
      filters: [order_items.created_date: "90 days"]
    }
  }
  # Add define view's fields as desired
  dimension: user_id {
    hidden: yes
  }
  dimension: number_of_orders_90_day {
    type: number
  }
  dimension: customer_value_90_day {
    type: number
  }
}

為開發模式建立篩選器

有時您建立的原生衍生資料表需要很長時間才能產生,如果您在開發模式中測試大量變更,這可能會很耗時。在這些情況下,您可以使用 dev_filters 建立原生衍生資料表的較小開發版本:

view: e_faa_pdt {
  derived_table: {
  ...
    datagroup_trigger: e_faa_shared_datagroup
    explore_source: flights {
      dev_filters: [flights.event_date: "90 days"]
      filters: [flights.event_date: "2 years", flights.airport_name: "Yucca Valley Airport"]
      column: id {}
      column: airport_name {}
      column: event_date {}
    }
  }
...
}

這個範例包含 dev_filters 參數,可將資料篩選範圍設為過去 90 天,以及 filters 參數,可將資料篩選範圍設為過去 2 年和 Yucca Valley 機場。dev_filters 參數會與 filters 參數搭配運作,以便將所有篩選條件套用至資料表的開發版本。如果 dev_filtersfilters 都為同一欄指定篩選器,則表格的開發版本會優先採用 dev_filters。在這個範例中,表格的開發版本會篩選出 Yucca Valley Airport 最近 90 天的資料。

如果原生衍生資料表含有 dev_filters 參數,開發資料表就無法做為正式版,因為開發版本含有縮寫的資料集。如果是這種情況,開發完資料表後,您可以在部署變更前註解掉 dev_filters 參數,然後在開發模式中查詢資料表。部署變更時,Looker 會建構完整版本的資料表,供實際工作環境使用。如要進一步瞭解如何在正式版中使用開發資料表,請參閱「Looker 中的衍生資料表」說明文件頁面。

請注意,反過來說也是如此:如果您有含 dev_filters 參數的原生衍生資料表,並在開發模式中查詢該資料表,Looker 可以使用正式環境資料表來回答開發模式查詢。除非您變更資料表的定義,然後在開發模式中查詢資料表,否則這項陳述為真。此時,Looker 會建立開發資料表來回答查詢。

將篩選器傳遞至原生衍生資料表

在探索中加入原生衍生資料表時,您可以從探索中取得執行階段篩選器,並套用至原生衍生資料表查詢。方法是在原生衍生資料表的 explore_source 中加入 bind_all_filtersbind_filters 參數。

從「探索」將執行階段篩選器傳遞至原生衍生資料表子查詢時,執行階段篩選器必須位於與原生衍生資料表彙整至相同「探索」的檢視區塊中。此外,由於原生衍生資料表必須在執行階段重新產生,才能納入「探索」的執行階段篩選器,因此原生衍生資料表不能是永久衍生資料表 (PDT)

正在使用 bind_all_filters

如要將篩選器從「探索」傳遞至原生衍生資料表子查詢,最簡單的方法是在原生衍生資料表的 explore_source 參數中指定 bind_all_filters: yes。這會將「探索」的所有執行階段篩選器傳遞至原生衍生資料表子查詢。

具有 bind_all_filters: yes 的原生衍生資料表必須加入原生衍生資料表 explore_source 參數中指定的相同「探索」。如要在其他探索中使用原生衍生資料表,請改用 bind_filters 參數,如「使用 bind_filters」一節所述。

以下是含有 bind_all_filters: yes 的原生衍生資料表 LookML:


view: top_10_simple_item_names {
  view_label: "Top 10s"
  derived_table: {
    explore_source: order_items {
      column: total_sale_price {
        field: order_items.total_sale_price
      }
      column: item_name {
        field: products.item_name
      }
      derived_column: rank {
        sql: RANK() OVER (ORDER BY total_sale_price DESC) ;;
      }
      bind_all_filters: yes
      sorts: [order_items.total_sale_price: desc]
      timezone: "query_timezone"
      limit: 10
    }
  }
  dimension: item_name {
    group_label: "Simple Example"
  }
  dimension: rank {
    type: number
    group_label: "Simple Example"
  }
  dimension: item_name_ranked {
    group_label: "Simple Example"
    order_by_field: rank
    type: string
    sql: ${rank} || ') ' || ${item_name} ;;
  }
}

在原生衍生資料表的檢視畫面中,explore_sourceorder_items。以下是 order_items「探索」的 LookML,其中原生衍生資料表會加入「探索」:

explore: order_items {
  ...
  join: top_10_simple_item_names {
    type: inner
    relationship: many_to_one
    sql_on: ${products.item_name} = ${top_10_simple_item_names.item_name} ;;
  }
}

如要查看這個範例的實際運作情形,請參閱「[Analytic Block] – Pivot by Top X – Introducing bind_all_filters: yes」社群貼文。

正在使用 bind_filters

explore_sourcebind_filters 子參數會將探索查詢中的特定篩選器傳遞至原生衍生資料表子查詢:

  • to_field 是原生衍生資料表中的欄位,篩選器會套用至該欄位。to_field 必須是基礎 explore_source 的欄位。
  • 如果使用者在執行階段指定篩選器,from_field 會指定要從哪個探索欄位取得篩選器。

在執行階段,探索中的 from_field 上的任何篩選器都會傳遞至原生衍生資料表子查詢中的 to_field

下列 LookML 顯示含有 bind_filters 的原生衍生資料表。完成這項設定後,Looker 會將套用至「探索」中 filtered_lookml_dt.filter_date 欄位的任何篩選器,套用至原生衍生資料表中的 users.created_date 欄位。

derived_table: {
  explore_source: order_items {
    bind_filters: {
      to_field: users.created_date
      from_field: filtered_lookml_dt.filter_date
    . . .
    }
  }
}

注意事項

只使用一個 explore_source 參數

每個原生衍生資料表只接受一個 explore_source 參數。後續的 explore_source 參數不會引發 LookML 驗證錯誤,但會覆寫先前的 explore_source 參數。

如要從不同檢視表中的欄位建立資料欄,請先在同一個探索中彙整不同檢視表。然後使用該「探索」頁面進行 explore_source

使用 include 陳述式啟用參照欄位

建立原生衍生資料表時,您必須加入包含探索定義的檔案。如要達到這個目的,最佳做法是在獨立的探索檔案中定義探索,如「建立探索檔案」說明文件所述。

如果您建立獨立的「探索」檔案:

  • 原生衍生資料表的檢視區塊檔案應包含該探索的檔案。例如:
    • include: "/explores/order_items.explore.lkml"
  • Explore 的檔案應包含所需檢視區塊檔案。例如:
    • include: "/views/order_items.view.lkml"
    • include: "/views/users.view.lkml"
  • 模型應包含探索的檔案。例如:
    • include: "/explores/order_items.explore.lkml"