Google cloud platform 如何从按日期和限制1排序的BigQuery SQL中的联接中提取最早的子记录
我正在使用GCP中的BiqQuery分析Shopify模式数据,并尝试将Shopify客户数据加入到子记录(即订单数据)中,以便我可以找到在特定时间创建的每个客户的第一个订单,但将其限制为1…就像asc创建的订单一样,限制为1。我真的没办法弄明白Google cloud platform 如何从按日期和限制1排序的BigQuery SQL中的联接中提取最早的子记录,google-cloud-platform,google-bigquery,shopify,Google Cloud Platform,Google Bigquery,Shopify,我正在使用GCP中的BiqQuery分析Shopify模式数据,并尝试将Shopify客户数据加入到子记录(即订单数据)中,以便我可以找到在特定时间创建的每个客户的第一个订单,但将其限制为1…就像asc创建的订单一样,限制为1。我真的没办法弄明白 Select customers.created_at as customer, customers.orders_count, orders.created_at, orders.order_number FROM `shopxxx.shopif
Select
customers.created_at as customer,
customers.orders_count,
orders.created_at,
orders.order_number
FROM `shopxxx.shopify.customers` as customers
Join `shopxxx.shopify.orders` as orders
on customer = orders.customer
WHERE
customers.orders_count != 0
AND customers.created_at > "2018-09-12 00:00:00 UTC" and customers.created_at < "2019-09-12 23:59:59 UTC"
and orders.source_name != 'web'
order by customers.created_at desc
下面是BigQuery标准SQL
#standardSQL
SELECT
customers.customer_id,
customers.created_at AS customer,
customers.orders_count,
ARRAY_AGG(STRUCT(orders.created_at AS created_at, orders.order_number AS order_number) ORDER BY orders.created_at LIMIT 1)[OFFSET(0)].*
FROM `shopxxx.shopify.customers` AS customers
JOIN `shopxxx.shopify.orders` AS orders
ON customers.customer_id = orders.customer_id
WHERE customers.orders_count != 0
AND customers.created_at > "2018-09-12 00:00:00 UTC" AND customers.created_at < "2019-09-12 23:59:59 UTC"
AND orders.source_name != 'web'
GROUP BY 1, 2, 3
ORDER BY customers.created_at DESC
还是不同的方法
#standardSQL
SELECT
customers.customer_id,
customers.created_at AS customer,
customers.orders_count,
orders.created_at AS created_at,
orders.order_number AS order_number
FROM `shopxxx.shopify.customers` AS customers
JOIN (
SELECT customer_id, ARRAY_AGG(STRUCT(created_at, order_number) ORDER BY created_at LIMIT 1)[OFFSET(0)].*
FROM `shopxxx.shopify.orders`
WHERE source_name != 'web' GROUP BY customer_id
) AS orders
ON customers.customer_id = orders.customer_id
WHERE customers.orders_count != 0
AND customers.created_at > "2018-09-12 00:00:00 UTC" AND customers.created_at < "2019-09-12 23:59:59 UTC"
ORDER BY customers.created_at DESC
哇!这为我指明了学习的方向。我知道我今晚要做什么。