简体   繁体   中英

I need to group by year and month from a timestamp structured as yyyyMMddHHmmss 20170227141500

I need to group by year and month from a timestamp, structured as yyyyMMddHHmmss 20170227141500 (Google's bigquery gkg table). I need to count the year monthly records. Data type is integer. timestamp is the column name. In fact, I need to gruop by first 6 digits of the timestamp. yearmonth is my group name, not included in the table.

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

group by GKGRECORDID, themes, yearmonth

It is a number so you cannot use date/time functionality. You can convert to a string and take the leftmost 6 characters:

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

You should fix the data to store date/time values as either datetime s or timestamp s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM