使用 CTE 过滤数据
使用 CTE 的高效方式之一,是先在 CTE 中过滤数据,再在后续查询中使用。这样可以减少进入最终查询的数据量,从而降低整体查询成本。本题的查询将帮助我们找出包含单价超过 $150 的商品的订单,其对应的订单状态。
本练习是课程的一部分
BigQuery 入门
练习说明
- 创建一个新的 CTE
orders,筛选出价格超过 $150 的订单,并对order_items执行 UNNEST 以获取price列。 - 将该 CTE 的结果与
ecomm_order_details数据集进行 JOIN,统计各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