การกรองข้อมูลด้วย CTE
หนึ่งในวิธีที่มีประสิทธิภาพที่สุดในการใช้ CTE คือการกรองข้อมูลใน CTE ก่อน แล้วค่อยนำไปใช้ในส่วนถัดไปของคิวรี วิธีนี้ช่วยลดต้นทุนโดยรวมของคิวรี เพราะนำข้อมูลเข้าสู่คิวรีสุดท้ายน้อยลง คิวรีนี้จะช่วยให้เราค้นหาสถานะของคำสั่งซื้อที่มีรายการสินค้าราคาเกิน $150
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
BigQuery เบื้องต้น
คำแนะนำการฝึกหัด
- สร้าง CTE ใหม่ชื่อ
ordersสำหรับคำสั่งซื้อที่มีราคาเกิน $150 โดย un-nestorder_itemsเพื่อเข้าถึงคอลัมน์price - Join ผลลัพธ์ของ CTE กับชุดข้อมูล
ecomm_order_detailsเพื่อหาจำนวนorder_statusและรวมกลุ่มด้วยการCOUNTคำสั่งซื้อในแต่ละสถานะ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
-- Add the correct items to finish our filtered CTE
-- Create a new CTE with the name orders
WITH ___ AS (
SELECT order_id
-- Add the correct column for the order item details
FROM ecommerce.ecomm_orders, UNNEST(___) items
-- Fill in the correct column for the item price
WHERE items.___ > 150
)
SELECT
-- Aggregate to find the total number of orders
___(order_id),
-- Add the column for the status of the order
___
FROM ecommerce.ecomm_order_details od
JOIN orders o USING (order_id)
GROUP BY order_status