使用 CTE 进行优化
CTE 还能通过在主查询处理数据之前完成诸如聚合之类的计算,来帮助您优化查询。
例如,您想按订单包含的商品数量,找出付款次数的最大值。本练习将向您展示如何用 CTE 高效完成这一点。
本练习是课程的一部分
BigQuery 入门
练习说明
- 在子查询和主查询中添加正确的聚合函数。
- 在主查询中两次使用
order_items列,并用ARRAY_LENGTH计算每个订单的商品数量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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)