RANK và LEAD/LAG
Xếp hạng cho phép bạn sắp xếp dữ liệu theo bất kỳ thứ tự nào, trong khi các hàm LAG và LEAD giúp bạn nhìn lùi hoặc tiến trong cửa sổ của truy vấn. Những công cụ này hỗ trợ bạn tạo báo cáo thuyết phục và các phân tích phức tạp hơn với LAG/LEAD. Tại đây, bạn sẽ thực hành chúng với dữ liệu của chúng ta.
Bài tập này là một phần của khóa học
Giới thiệu về BigQuery
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
-- 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;