เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

RANK และ LEAD/LAG

การจัดอันดับช่วยให้สามารถเรียงลำดับข้อมูลตามเงื่อนไขที่ต้องการได้ ส่วนฟังก์ชัน LAG และ LEAD ช่วยให้มองย้อนกลับหรือมองไปข้างหน้าในหน้าต่างของคิวรีได้ เครื่องมือเหล่านี้มีประโยชน์ในการสร้างรายงานที่น่าสนใจ รวมถึงฟังก์ชันวิเคราะห์ที่ซับซ้อนในกรณีของ 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;
แก้ไขและรันโค้ด