Skip to content
Advertisement

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

Advertisement

Answer

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 datetimes or timestamps.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement