시작하기무료로 시작하기

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;
코드 편집 및 실행