【发布时间】:2016-06-27 23:40:35
【问题描述】:
我想从日期字段(“yyyy-mm-dd”)或时间戳字段中按“yyyy-mm”分组,这样我就可以提取和分组多年的交易数据,而不必提取单独的查询分组每年的月份。
【问题讨论】:
标签: date google-bigquery
我想从日期字段(“yyyy-mm-dd”)或时间戳字段中按“yyyy-mm”分组,这样我就可以提取和分组多年的交易数据,而不必提取单独的查询分组每年的月份。
【问题讨论】:
标签: date google-bigquery
SELECT
CONCAT(STRING(YEAR(timestamp)),'-',RIGHT(STRING(100 + MONTH(timestamp)), 2)) AS yyyymm,
<any aggregations here>
FROM YourTable
GROUP BY 1
另一种选择:
SELECT
STRFTIME_UTC_USEC(timestamp, "%Y-%m") AS yyyymm,
<any aggregations here>
FROM YourTable
GROUP BY 1
两个版本都应该使用时间戳或日期
【讨论】:
您可以将以下内容用于标准 SQL:
SELECT concat(cast(format_date("%E4Y", cast(current_date() as date)) as string),'-',cast(format_date("%m", cast(current_date() as date)) as string)) as yyyymm, <other aggregations>
FROM <YourTable>
GROUP BY 1;
只需将 current_date() 替换为包含时间戳的列名即可。
【讨论】: