始める無料で始める

CTE でデータをフィルタリングする

CTE の効果的な使い方のひとつは、後続のクエリで使う前に、CTE の中でデータを絞り込むことです。これにより、最終的なクエリに取り込むデータ量が減り、クエリのコストを下げられます。次のクエリでは、価格が $150 を超える商品を含む注文の注文ステータスを調べます。

この演習はコースの一部です

BigQuery入門

コースを見る

演習の手順

  • 新しい CTE orders を作成し、価格が $150 を超える注文のみを対象にします。その際、order_items を UNNEST して price 列を参照できるようにしてください。
  • CTE の結果を ecomm_order_details データセットに結合し、order_status ごとの件数を求めます。各ステータスの注文数を COUNT で集計してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

-- Add the correct items to finish our filtered CTE

-- Create a new CTE with the name orders
WITH ___ AS (
  SELECT order_id
  -- Add the correct column for the order item details
  FROM ecommerce.ecomm_orders, UNNEST(___) items
  -- Fill in the correct column for the item price 
  WHERE items.___ > 150
)

SELECT
	-- Aggregate to find the total number of orders
	___(order_id),
	-- Add the column for the status of the order
	___
FROM ecommerce.ecomm_order_details od
JOIN orders o USING (order_id)
GROUP BY order_status
コードを編集して実行