【问题标题】:I need to group by year and month from a timestamp structured as yyyyMMddHHmmss 20170227141500我需要从结构为 yyyyMMddHHmmss 20170227141500 的时间戳中按年和月分组
【发布时间】:2021-02-10 21:45:50
【问题描述】:

我需要从时间戳按年和月分组,结构为 yyyyMMddHHmmss 20170227141500(Google 的 bigquery gkg 表)。我需要计算年份的每月记录。数据类型为整数。时间戳是列名。事实上,我需要按时间戳的前 6 位进行分组。 yearmonth 是我的组名,不包含在表中。

SELECT count(GKGRECORDID), themes, yearmonth
FROM `gdelt-bq.gdeltv2.gkg` 
CONCAT(yearmonth( timestamp))

group by GKGRECORDID, themes, yearmonth

【问题讨论】:

  • 列的数据类型是什么>

标签: sql google-bigquery


【解决方案1】:

这是一个数字,因此您不能使用日期/时间功能。可以转换成字符串,取最左边的6个字符:

SELECT themes, left(cast(timestamp as string), 6) as yearmonth,
       count(GKGRECORDID)
FROM `gdelt-bq.gdeltv2.gkg` 
GROUP BY themes, yearmonth;

您应该修复数据以将日期/时间值存储为datetimes 或timestamps。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    相关资源
    最近更新 更多