開始使用免費開始

使用 CTE 最佳化

CTE 也能幫助你最佳化查詢:先在進入主查詢前處理像彙總這類的計算。

例如,你想依訂單品項數找出最高的付款次數。這個練習會示範如何用 CTE 高效完成。

本練習屬於課程

BigQuery 入門

檢視課程

練習說明

  • 在子查詢與主查詢中加入正確的彙總函式。
  • 在主查詢中使用 ARRAY_LENGTH 並以 order_items 欄位各使用一次,來找出每筆訂單的品項數。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

WITH payments AS (
  SELECT
    -- Use the correct aggregate to find the highest number of payments
    ___(payment_sequential) AS num_payments,
    order_id
  FROM ecommerce.ecomm_payments 
  -- Group the results by order
  GROUP BY ___)
     
SELECT
  -- Add the correct function to find the length or number of order items
  ___(o.order_items) AS num_items,
  MAX(p.num_payments) AS max_payments
FROM ecommerce.ecomm_orders o
JOIN payments p
-- Add the correct keyword to join using the same column
___ (order_id)
-- Add the correct function to find the length or number of order items
GROUP BY ___(o.order_items)
編輯並執行程式碼