การปรับแต่งคิวรีด้วย CTE
CTE ช่วยให้ปรับแต่งคิวรีได้อย่างมีประสิทธิภาพ โดยจัดการการคำนวณ เช่น การรวมกลุ่มข้อมูล ก่อนที่จะนำข้อมูลไปใช้ในคิวรีหลัก
ตัวอย่างเช่น หากต้องการหาจำนวนการชำระเงินสูงสุดตามจำนวนรายการในคำสั่งซื้อ แบบฝึกหัดนี้จะแสดงวิธีทำสิ่งนั้นได้อย่างมีประสิทธิภาพด้วย CTE
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
BigQuery เบื้องต้น
คำแนะนำการฝึกหัด
- เพิ่มฟังก์ชัน aggregate ที่ถูกต้องในคิวรีย่อยและคิวรีหลัก
- ใช้
ARRAY_LENGTHเพื่อหาจำนวนรายการในแต่ละคำสั่งซื้อโดยอ้างอิงคอลัมน์order_itemsสองครั้งในคิวรีหลัก
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
WITH payments AS (
SELECT
-- Use the correct aggregate to find the highest number of payments
___(payment_sequential) AS num_payments,
order_id
FROM ecommerce.ecomm_payments
-- Group the results by order
GROUP BY ___)
SELECT
-- Add the correct function to find the length or number of order items
___(o.order_items) AS num_items,
MAX(p.num_payments) AS max_payments
FROM ecommerce.ecomm_orders o
JOIN payments p
-- Add the correct keyword to join using the same column
___ (order_id)
-- Add the correct function to find the length or number of order items
GROUP BY ___(o.order_items)