教學:建立一個包含連接與資料建模的度量視圖

在這個教學中,你會在 TPC-H 資料集上建立銷售分析指標視圖。 最後,你會得到一個度量視角:

  • 利用雪花架構將訂單和客戶跨多個表格串連。
  • 定義時間、地理和秩序屬性的欄位(也稱為維度)。
  • 計算簡單與複雜的指標,包括比率、過濾聚合及視窗測量。
  • 利用可組合性從較簡單的指標建立複雜的指標。
  • 定義一個參數,在查詢時套用貼現率。
  • 包含供儀表板和 AI 工具使用的代理程式中繼資料。

如果你是度量視圖新手,可以從 「建立度量視圖 」開始學習基礎。 這篇教程在基礎上進一步擴展,涵蓋實際應用中的複雜性。

要求

若要完成本教學課程,您必須具備:

  • 已啟用 Unity Catalog 的工作區。
  • 運行 Databricks Runtime 17.3 或以上版本的 SQL 倉庫或運算資源。

如需建立度量視圖所需的完整權限清單,請參閱 前置條件。

Note

Databricks Runtime 16.4 及以上版本支援建立度量檢視。 本教學使用的功能需要 Databricks 執行時 17.3 或以上,且部分步驟需支援較晚的執行環境。 關於每個功能的最小執行時間,請參見 「指標檢視功能可用性」。

資料模型

TPC-H 資料集模擬了一條批發供應鏈。 這個教學使用三個以雪花結構連接的表格:

  • orders在customer上連接至o_custkey = c_custkey
  • customer在nation上連接至c_nationkey = n_nationkey
資料表 Role 關鍵資料行
orders 事實表(訂單交易) o_orderkey、、 o_custkey、 o_totalprice、 o_orderdate、 o_orderstatus
customer 維度表(客戶資料) c_custkey、c_name、c_mktsegment、c_nationkey
nation 尺寸表(國家或地區參考) n_nationkey、n_name、n_regionkey

步驟 1:建立度量檢視並開啟編輯器

你可以在目錄總管介面中建立這個度量檢視,使用 Genie Code 產生,或直接撰寫完整的 YAML 定義。 這三種方法都解析為單一的 YAML 定義,以建模度量視圖。 在接下來的每個步驟中,選擇目錄 總管介面 或 YAML 編輯器 標籤,依照您偏好的方法操作。 如果你使用 YAML 編輯器,每個步驟中的範例程式碼都是 YAML 定義中對應於你在該步驟中建立內容的那一部分。

Note

本教學中的 YAML 範例使用 fields 關鍵字。 當你在低程式碼編輯器中建立度量視圖時,所產生的 YAML 會改為使用對應的 dimensions 關鍵字。 參見菲爾茲。

如果你不熟悉建立度量視圖的介面,請參閱 「建立度量視圖」。

若要建立度量檢視,請在 Catalog Explorer 中:

  1. 搜尋 samples.tpch.orders。
  2. 按一下表格名稱。
  3. 點選 「建立指標>檢視 」並命名檢視。

關於詳細的建立步驟,請參見 建立指標視圖。 編輯器開啟時,使用 介面 標籤進行互動式建置,或點擊 <> 按鈕直接編輯 YAML 定義。

步驟 2:設定度量視圖

為度量視圖設定版本和描述。 version 會決定 YAML 規範版本,而 comment 會說明度量檢視的用途,其內容會顯示於 Catalog Explorer 中。 Azure Databricks 會幫你管理這個版本。

目錄檔案總管使用者介面

這個版本已為你定義好。 儲存指標檢視後,若要新增或編輯描述:

  1. 在目錄總管中,搜尋該度量檢視並按一下其名稱。
  2. 點選 描述,然後輸入度量檢視的描述。 你可以使用 YAML編輯器 標籤中所示的範例描述。

此文字對應 comment 於 YAML 定義中的欄位。 如需更多編輯度量視圖的方法,請參見 「編輯度量視圖」。

YAML 編輯器

version: 1.1

comment: |-
  Sales analytics metric view for order performance analysis.
  Joins orders with customers and geography.
  Owner: Analytics Team
  Last updated: 2025-01-15

步驟 3:定義來源與連接

定義主要來源資料表並連接相關資料表:

  • source 將事實表(順序)設定為顆粒。
  • joins 透過多對一關係帶來客戶資料。
  • 巢狀 nation 連接展現了雪花式結構模式,透過連接 customer 來到達地理資料,其中國家是客戶的子維度。

目錄檔案總管使用者介面

此範例新增兩個 多對一 連接,用來建立雪花式綱要。

若要加入 customer 連接:

  1. 在編輯器中,點擊右上角的 「加入 」即可開啟 「新增加入 」對話框。
  2. 搜尋 samples.tpch.customer,按一下資料表名稱,然後按一下 新增。
  3. 將連接條件設為 o_custkey = c_custkey。
  4. 在 聯結基數下,選擇 多對一。 關於選擇基數的指引,請參見 「加入基數」。

然後加入巢狀 nation 連接。 重複執行從 customer 聯結開始的步驟,在 samples.tpch.nation 上聯結 c_nationkey = n_nationkey。 將聯結巢狀置於 customer 之下,會把 nation 建模為 customer 的子維度。

完整的連接對話步驟,請參見 步驟 2:新增連接。

YAML 編輯器

source: SELECT * FROM samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    'on': o_custkey = c_custkey
    joins:
      - name: nation
        source: samples.tpch.nation
        'on': c_nationkey = n_nationkey

步驟 4:定義濾波器

A filter 限制來源資料,且適用於所有度量視圖上的查詢。 本教學將度量視角限制於近期資料。

目錄檔案總管使用者介面

定義濾波器:

  1. 在編輯器中,點擊篩選圖示。右上角的濾鏡。
  2. 使用下拉選單將 欄位 設為 o_orderdate,運算 子 設為 >=, 值設為1995-01-01。

欲了解更多關於濾波器的資訊,請參見 步驟 3:定義濾波器。

YAML 編輯器

filter: o_orderdate >= '1995-01-01'

步驟五:定義欄位

欄位是使用者群組和篩選的屬性。 欄位可以是類別欄位(如區域或狀態),或是使用者在查詢時彙整的未彙總數字欄位(如年齡或數量)。

代理程式中繼資料

本教學中的每個欄位和度量都包含代理程式中繼資料屬性,可改善你的指標檢視與儀表板及 AI 工具的搭配運作:

  • display_name: 一個可讀的標籤,出現在視覺化中,取代技術欄位名稱。
  • synonyms: 幫助 Genie 等 AI 工具透過自然語言查詢發現欄位與度量的替代名稱。
  • format: 值如何在下游表面(如儀表板、筆記本及 SQL 查詢結果)中顯示,例如以貨幣、數字或百分比。

這些屬性為可選,但建議使用。 以下步驟中的欄位和度量定義會以內嵌方式包含在其中。

欄位定義

這個教學新增:

  • 時間欄位:order_date、order_month以及order_year,以多種粒度支援不同的分析需求。
  • 轉換後的欄位:order_status 和 order_priority,其使用 CASE 和 SPLIT 將來源代碼轉換為可讀取的標籤。
  • 聯結欄位:customer_name、market_segment以及customer_nation,這些欄位使用聯結名稱來參照聯結資料表。 巢狀連接欄位使用鏈點符號,例如 customer.nation.n_name,來遍歷雪花結構。

目錄檔案總管使用者介面

編輯器會自動將所有來源欄位加入至 Fields 分頁。 編輯、重新命名、移除並新增欄位,讓度量檢視能定義以下內容。 對於每個欄位,按一下其名稱即可編輯,或按一下 新增或加號圖示新增 以建立欄位,然後在 建立器 或 自訂 模式中設定運算式。 如圖所示,為每個欄位設定 顯示名稱 和 同義詞 。

  1. order_date:在 Builder 模式中,選取 o_orderdate 欄。 將顯示名稱設為 Order Date。

  2. order_month:在 自訂 模式中,輸入 DATE_TRUNC('MONTH', order_date)。 將顯示名稱設為 Order Month。

  3. order_year:在 自訂 模式中,輸入 YEAR(order_date)。 將顯示名稱設為 Order Year。

  4. order_status:在 自訂 模式中,輸入以下表達式。 將顯示名稱設為 Order Status ,同義詞設為 status, fulfillment status。

    CASE o_orderstatus
      WHEN 'O' THEN 'Open'
      WHEN 'P' THEN 'Processing'
      WHEN 'F' THEN 'Fulfilled'
    END
    
  5. order_priority:在 自訂 模式中,輸入 SPLIT(o_orderpriority, '-')[0]。 將顯示名稱設為 Priority。

  6. customer_name:在Builder模式中,從已聯結的c_name資料表選取customer欄。 將顯示名稱設為 Customer Name。

  7. market_segment:在 Builder 模式中,從已聯結的 c_mktsegment 資料表選取 customer 欄位。 將顯示名稱設為 Market Segment ,同義詞設為 segment, industry。

  8. customer_nation:在 自訂 模式中,輸入 customer.nation.n_name 以參照巢狀 nation 聯結。 將顯示名稱設為 Country ,同義詞設為 nation, country。

完整欄位步驟請參見 步驟4:新增欄位。

YAML 編輯器

fields:
  - name: order_date
    expr: o_orderdate
    display_name: Order Date

  - name: order_month
    expr: "DATE_TRUNC('MONTH', order_date)"
    display_name: Order Month

  - name: order_year
    expr: YEAR(order_date)
    display_name: Order Year

  - name: order_status
    expr: |-
      CASE o_orderstatus
        WHEN 'O' THEN 'Open'
        WHEN 'P' THEN 'Processing'
        WHEN 'F' THEN 'Fulfilled'
      END
    display_name: Order Status
    synonyms:
      - status
      - fulfillment status

  - name: order_priority
    expr: "SPLIT(o_orderpriority, '-')[0]"
    display_name: Priority

  - name: customer_name
    expr: customer.c_name
    display_name: Customer Name

  - name: market_segment
    expr: customer.c_mktsegment
    display_name: Market Segment
    synonyms:
      - segment
      - industry

  - name: customer_nation
    expr: customer.nation.n_name
    display_name: Country
    synonyms:
      - nation
      - country

步驟6:定義參數

參數讓你在查詢指標時能將值傳遞到度量視圖,因此單一定義可以服務多種查詢變體。 本教學加入了一個 discount 參數,供後續的量值用來計算折現營收。 該參數預設為 0,因此未傳遞值的查詢會返回未折扣的收益。 欲了解更多參數,請參見 「使用參數與度量視圖」。

目錄檔案總管使用者介面

在編輯器標題中,點選 「新增參數」。 輸入 discount 名稱,然後輸入預設值, 0 並選擇資料型別 double 。

YAML 編輯器

parameters:
  - name: discount
    data_type: double
    default: 0

步驟7:定義衡量標準

衡量指標是使用者想要分析的計算。 先定義原子度量,然後利用可組合特性建立複雜度量,這些度量會參考先前由MEASURE()函數定義的度量。 如 display_name所述,為每個量值設定 format、synonyms 和 。 這個教學新增:

  • 原子量值:order_count、total_revenue和unique_customers,是構成基礎的簡單彙總。
  • 組合度量:avg_order_value 和 revenue_per_customer,其會參照先前定義的度量 MEASURE(),而不是重複撰寫彙總邏輯。 若 total_revenue 有變動,這些指標會自動使用更新後的定義。 請參閱 組合性。
  • 篩選式量值:open_order_revenue 和 fulfilled_order_revenue,使用 FILTER (WHERE ...) 建立不需另外建立欄位的條件量值。
  • 參數化測度:discounted_revenue,參考 discount 該參數以應用貼現率。 請參見 「使用參數與度量視圖」。
  • 視窗度量:t7d_customers,其會計算 7 天內不重複客戶的滾動計數。 更多窗戶度量模式請參見 窗戶度量 。

目錄檔案總管使用者介面

編輯器會自動加入範例 COUNT(*) 測度。 編輯或移除該項目,並新增測量,讓度量檢視明確定義為以下內容。 對每個度量點選 新增或加號圖示新增,然後在 建構 器或 自訂 模式中設定表達式。 請依圖中設定 顯示名稱、 格式與 同義 詞。 貨幣格式用小數點2位,數字用0位小數點。

  1. order_count:在建立器模式中,選取的o_orderkey彙總。 將顯示名稱設為 Order Count,格式改為 數字。
  2. total_revenue:在Builder模式中,於選擇o_totalprice彙總。 將顯示名稱設為 Total Revenue,格式為貨幣(USD),同義詞設為 revenue。 sales
  3. discounted_revenue:在 自訂 模式中,輸入 SUM(o_totalprice * (1 - discount))。 將顯示名稱設為 Discounted Revenue,格式為貨幣(USD)。
  4. unique_customers:在Builder模式中,於上選擇o_custkey彙總。 將顯示名稱設為 Unique Customers,格式改為 數字。
  5. avg_order_value:在 自訂 模式中,輸入 MEASURE(total_revenue) / MEASURE(order_count)。 將顯示名稱設為 Avg Order Value,格式為 貨幣(美元),同義詞設為 AOV。
  6. revenue_per_customer:在 自訂 模式中,輸入 MEASURE(total_revenue) / MEASURE(unique_customers)。 將顯示名稱設為 Revenue per Customer,格式為貨幣(USD)。
  7. open_order_revenue:在 自訂 模式中,輸入 SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')。 將顯示名稱設為 Open Order Revenue,格式為 貨幣(美元),同義詞設為 backlog。
  8. fulfilled_order_revenue:在 自訂 模式中,輸入 SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')。 將顯示名稱設為 Fulfilled Revenue,格式為貨幣(USD)。
  9. t7d_customers:在 自訂 模式中,輸入 COUNT(DISTINCT o_custkey)。 然後按一下 + 視窗,並設定一個依 order_date 排序、範圍為 trailing 7 day,且採用 last 半加法彙總的視窗。 將顯示名稱設為 7-Day Rolling Customers,格式改為 數字。

完整測量步驟請參見 步驟5:新增測量。

YAML 編輯器

measures:
  - name: order_count
    expr: COUNT(DISTINCT o_orderkey)
    display_name: Order Count
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
      abbreviation: compact

  - name: total_revenue
    expr: SUM(o_totalprice)
    display_name: Total Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - revenue
      - sales

  - name: discounted_revenue
    expr: SUM(o_totalprice * (1 - discount))
    display_name: Discounted Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact

  - name: unique_customers
    expr: COUNT(DISTINCT o_custkey)
    display_name: Unique Customers
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
      abbreviation: compact

  - name: avg_order_value
    expr: MEASURE(total_revenue) / MEASURE(order_count)
    display_name: Avg Order Value
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - AOV

  - name: revenue_per_customer
    expr: MEASURE(total_revenue) / MEASURE(unique_customers)
    display_name: Revenue per Customer
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact

  - name: open_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
    display_name: Open Order Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - backlog

  - name: fulfilled_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
    display_name: Fulfilled Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact

  - name: t7d_customers
    expr: COUNT(DISTINCT o_custkey)
    window:
      - order: order_date
        semiadditive: last
        range: trailing 7 day
    display_name: 7-Day Rolling Customers
    format:
      type: number
      decimal_places:
        type: exact
        places: 0

回顧完整定義

完成上述步驟後,你的度量視角會有以下完整定義:

查看完整的 YAML 定義
version: 1.1

parameters:
  - name: discount
    data_type: double
    default: 0

source: SELECT * FROM samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    'on': o_custkey = c_custkey
    joins:
      - name: nation
        source: samples.tpch.nation
        'on': c_nationkey = n_nationkey

filter: o_orderdate >= '1995-01-01'

comment: |-
  Sales analytics metric view for order performance analysis.
  Joins orders with customers and geography.
  Owner: Analytics Team
  Last updated: 2025-01-15

fields:
  - name: order_date
    expr: o_orderdate
    display_name: Order Date
  - name: order_month
    expr: "DATE_TRUNC('MONTH', order_date)"
    display_name: Order Month
  - name: order_year
    expr: YEAR(order_date)
    display_name: Order Year
  - name: order_status
    expr: |-
      CASE o_orderstatus
        WHEN 'O' THEN 'Open'
        WHEN 'P' THEN 'Processing'
        WHEN 'F' THEN 'Fulfilled'
      END
    display_name: Order Status
    synonyms:
      - status
      - fulfillment status
  - name: order_priority
    expr: "SPLIT(o_orderpriority, '-')[0]"
    display_name: Priority
  - name: customer_name
    expr: customer.c_name
    display_name: Customer Name
  - name: market_segment
    expr: customer.c_mktsegment
    display_name: Market Segment
    synonyms:
      - segment
      - industry
  - name: customer_nation
    expr: customer.nation.n_name
    display_name: Country
    synonyms:
      - nation
      - country

measures:
  - name: order_count
    expr: COUNT(DISTINCT o_orderkey)
    display_name: Order Count
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
      abbreviation: compact
  - name: total_revenue
    expr: SUM(o_totalprice)
    display_name: Total Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - revenue
      - sales
  - name: discounted_revenue
    expr: SUM(o_totalprice * (1 - discount))
    display_name: Discounted Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
  - name: unique_customers
    expr: COUNT(DISTINCT o_custkey)
    display_name: Unique Customers
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
      abbreviation: compact
  - name: avg_order_value
    expr: MEASURE(total_revenue) / MEASURE(order_count)
    display_name: Avg Order Value
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - AOV
  - name: revenue_per_customer
    expr: MEASURE(total_revenue) / MEASURE(unique_customers)
    display_name: Revenue per Customer
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
  - name: open_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
    display_name: Open Order Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - backlog
  - name: fulfilled_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
    display_name: Fulfilled Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
  - name: t7d_customers
    expr: COUNT(DISTINCT o_custkey)
    window:
      - order: order_date
        semiadditive: last
        range: trailing 7 day
    display_name: 7-Day Rolling Customers
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
使用 SQL 建立度量檢視

如果你是在目錄檔案總管之外建立這個定義,請執行以下 SQL 來建立度量檢視:

CREATE OR REPLACE VIEW catalog.schema.tpch_sales_analytics
WITH METRICS
LANGUAGE YAML
AS $$
version: 1.1

parameters:
  - name: discount
    data_type: double
    default: 0

source: SELECT * FROM samples.tpch.orders

joins:
  - name: customer
    source: samples.tpch.customer
    'on': o_custkey = c_custkey
    joins:
      - name: nation
        source: samples.tpch.nation
        'on': c_nationkey = n_nationkey

filter: o_orderdate >= '1995-01-01'

comment: |-
  Sales analytics metric view for order performance analysis.
  Joins orders with customers and geography.
  Owner: Analytics Team
  Last updated: 2025-01-15

fields:
  - name: order_date
    expr: o_orderdate
    display_name: Order Date
  - name: order_month
    expr: "DATE_TRUNC('MONTH', order_date)"
    display_name: Order Month
  - name: order_year
    expr: YEAR(order_date)
    display_name: Order Year
  - name: order_status
    expr: |-
      CASE o_orderstatus
        WHEN 'O' THEN 'Open'
        WHEN 'P' THEN 'Processing'
        WHEN 'F' THEN 'Fulfilled'
      END
    display_name: Order Status
    synonyms:
      - status
      - fulfillment status
  - name: order_priority
    expr: "SPLIT(o_orderpriority, '-')[0]"
    display_name: Priority
  - name: customer_name
    expr: customer.c_name
    display_name: Customer Name
  - name: market_segment
    expr: customer.c_mktsegment
    display_name: Market Segment
    synonyms:
      - segment
      - industry
  - name: customer_nation
    expr: customer.nation.n_name
    display_name: Country
    synonyms:
      - nation
      - country

measures:
  - name: order_count
    expr: COUNT(DISTINCT o_orderkey)
    display_name: Order Count
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
      abbreviation: compact
  - name: total_revenue
    expr: SUM(o_totalprice)
    display_name: Total Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - revenue
      - sales
  - name: discounted_revenue
    expr: SUM(o_totalprice * (1 - discount))
    display_name: Discounted Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
  - name: unique_customers
    expr: COUNT(DISTINCT o_custkey)
    display_name: Unique Customers
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
      abbreviation: compact
  - name: avg_order_value
    expr: MEASURE(total_revenue) / MEASURE(order_count)
    display_name: Avg Order Value
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - AOV
  - name: revenue_per_customer
    expr: MEASURE(total_revenue) / MEASURE(unique_customers)
    display_name: Revenue per Customer
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
  - name: open_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'O')
    display_name: Open Order Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
    synonyms:
      - backlog
  - name: fulfilled_order_revenue
    expr: SUM(o_totalprice) FILTER (WHERE o_orderstatus = 'F')
    display_name: Fulfilled Revenue
    format:
      type: currency
      currency_code: USD
      decimal_places:
        type: exact
        places: 2
      abbreviation: compact
  - name: t7d_customers
    expr: COUNT(DISTINCT o_custkey)
    window:
      - order: order_date
        semiadditive: last
        range: trailing 7 day
    display_name: 7-Day Rolling Customers
    format:
      type: number
      decimal_places:
        type: exact
        places: 0
$$;

關於建立其他度量視圖的方法,請參見 建立度量視圖。

步驟八:查詢指標視圖

使用商業友善的語法查詢度量視圖。 這個 MEASURE() 函數會依據你所選欄位的粒度彙總量值。

依維度彙總量值

此範例彙整了跨多個領域的測量。 它回傳總營收、訂單數及按客戶國家和市場區隔區劃分的平均訂單金額,並依營收最高排序:

SELECT
  customer_nation,
  market_segment,
  MEASURE(total_revenue) AS total_revenue,
  MEASURE(order_count) AS order_count,
  MEASURE(avg_order_value) AS avg_order_value
FROM catalog.schema.tpch_sales_analytics
GROUP BY customer_nation, market_segment
ORDER BY total_revenue DESC;

分析每月趨勢

此範例結合時間域與衡量趨勢。 它會依月回傳總營收與未結訂單收入(待訂單)及訂單狀態:

SELECT
  order_month,
  order_status,
  MEASURE(total_revenue) AS total_revenue,
  MEASURE(open_order_revenue) AS open_order_revenue
FROM catalog.schema.tpch_sales_analytics
GROUP BY order_month, order_status
ORDER BY order_month;

傳遞參數值

因為度量視圖定義了一個參數,你可以把它叫做一個以表格為值的函式,並在查詢時傳遞一個值。 以下查詢可享有10% 折扣。 由於 discount 預設值為 0,省略參數的查詢則返回未貼現的收入:

SELECT
  customer_nation,
  MEASURE(total_revenue) AS total_revenue,
  MEASURE(discounted_revenue) AS discounted_revenue
FROM catalog.schema.tpch_sales_analytics(discount => 0.1)
GROUP BY customer_nation
ORDER BY discounted_revenue DESC;

你學到了什麼

你建立了一個度量視圖,展示了:

Feature 範例
雪花模式結合 從客戶到國家的訂單(嵌套的多對一關聯)
時間欄位 日期、月份、年份的細緻度
已轉換的欄位 CASE 語句、 SPLIT 函數
簡單測量 COUNT、SUM
可組合性 avg_order_value 並參考 revenue_per_customer 透過 MEASURE() 使用之先前定義的度量
過濾度量 FILTER (WHERE ...) 條件彙總
窗戶測量 使用 7 天滾動客戶計數 trailing 7 day
參數 discount 參數套用於 discounted_revenue 度量
代理元資料 display_name, format, synonyms 關於域與測度

其他資源