Sql 在Google BigQuery中按用户和月份分组

Sql 在Google BigQuery中按用户和月份分组,sql,date,group-by,google-bigquery,Sql,Date,Group By,Google Bigquery,我对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

我对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 |
我想补充的是另一个按月分组。我希望每月有每个客户的总花费和订单数量(电子邮件)

根据更简单的方法,结果可能如下所示:

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|
或按行分组,如下所示:

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年,我的时间戳变量“创建时”如下所示:2020-01-02 16:20:12 UTC

我尝试过添加
groupbymoth(在创建时)
并使用
timestamp\u trunc
玩,但失败了

非常感谢您的帮助

多谢各位

为您的列类型使用或使用适当的函数:

select timestamp_trunc(created_at, month) as mon, 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 mon, email
order by mon, email;

嘿谢谢你的快速回复。我已经尝试了代码,但不幸的是它给出了以下错误:参数类型的函数DATE\u TRUNC没有匹配的签名:TIMESTAMP,DATE\u TIME\u PART。支持的签名:DATE\u TRUNC(DATE,DATE\u TIME\u PART)在[2:1]-您知道问题可能是什么吗?@emil\u rore。然后您需要
时间戳\u trunc()