Sử dụng WINDOW theo hàng
WINDOW functions thường dùng một cửa sổ dựa trên hàng, xem xét một số hàng trước hoặc sau hàng hiện tại. Trong bài tập này, bạn sẽ dùng cùng một truy vấn ba lần để tạo một window function dựa trên hàng.
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 find the rolling average
SELECT
order_id,
order_purchase_timestamp,
-- Aggregate the price to find the average item price
AVG(___)
OVER(
-- Order the query by the purchase timestamp
ORDER BY ___
-- Start the rows window as a between statement
ROWS ___
-- Create the window for the nine previous rows
___ ___
-- Stop the window at the current row
AND ___ ROW) as rolling_avg
FROM ecommerce.ecomm_order_details od
JOIN ecommerce.ecomm_orders o
USING (order_id), unnest(o.order_items) as item
ORDER BY order_purchase_timestamp;