始める無料で始める

RANK と LEAD/LAG

ランキングを使うと、データを任意の順序で並べ替えて順位付けできます。LAGLEAD 関数を使うと、クエリのウィンドウ内で前後の行を参照できます。これらのツールは、説得力のあるレポート作成や、LAG/LEAD を用いた高度な分析に役立ちます。ここでは、これらを実際にデータで使ってみましょう。

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

BigQuery入門

コースを見る

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

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

-- Complete the query to order customers by the total amount spent

-- First, write a CTE to group customers and find their total amount spent
WITH orders AS (
  SELECT
  -- Add the correct columns and aggregate functions
  ___,
  SUM(___) as all_items
  FROM ecommerce.ecomm_orders o, UNNEST(o.order_items) items
  JOIN ecommerce.ecomm_order_details od USING (order_id)
  GROUP BY od.customer_id
)

SELECT
	customer_id,
	all_items,
-- Fill in the RANK window function and OVER clause
RANK() ___(ORDER BY ___ DESC)
FROM orders
ORDER BY all_items DESC;
コードを編集して実行