【发布时间】:2021-02-12 19:46:55
【问题描述】:
我是 BigQuery 的新手,我编写了以下查询:
select
email,
min(address.zip) as zip,
min(country) as country,
min(source),
min(created_at) as first_order,
max(created_at)as last_order,
count(order_number) as number_orders,
sum(total_price) as total_spent
from orders_data
group by email
它给我的是一个表格,其中所有订单都按客户分组,输出如下:
email | zip | country | source | first_order | last_order | number_orders | total_spent |
我想补充的是另一个按月分组。我希望每月获得每位客户(电子邮件)的 total_spend 和 number_orders。
根据更容易的情况,结果可能如下所示:
email | zip | country | source | first_order | last_order | number_orders | total_spent | 2017_January_spent | 2017_January_num_orders | 2017_February_spent | 2017_February_num_orders | ... |
或按行分组,如下所示:
month | email | zip | country | source | first_order | last_order | number_orders | total_spent | 2017_January_spent | 2017_January_num_orders | 2017_February_spent | 2017_February_num orders | ... |
01.2017| cust_A|
02.2017| cust_A|
03.2017| cust_A|
不确定是否有帮助,但我的数据范围从 2017 年到 2020 年,我的时间戳变量“created_at”如下所示:2020-01-02 16:20:12 UTC
我已经尝试通过添加group by moth(created_at) 并且还使用timestamp_trunc 但失败了。
我们将不胜感激!
非常感谢!
【问题讨论】:
标签: sql date group-by google-bigquery