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