【问题标题】:Google BigQuery: Select and group by "yyyy-mm" from date field ("yyyy-mm-dd") or timestampGoogle BigQuery:从日期字段(“yyyy-mm-dd”)或时间戳中选择并按“yyyy-mm”分组
【发布时间】:2016-06-27 23:40:35
【问题描述】:

我想从日期字段(“yyyy-mm-dd”)或时间戳字段中按“yyyy-mm”分组,这样我就可以提取和分组多年的交易数据,而不必提取单独的查询分组每年的月份。

【问题讨论】:

    标签: date google-bigquery


    【解决方案1】:
    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 还是用于旧 sql?
    • 那是在 Legacy SQL 时代 - 所以它适用于 Legacy
    【解决方案2】:

    您可以将以下内容用于标准 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() 替换为包含时间戳的列名即可。

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 2013-10-19
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      相关资源
      最近更新 更多