我使用标准SQL for Bigquery。我需要找到早上,下午或晚上发生的所有事件。我可以做这样的事情
WHERE TIMESTAMP_MICROS(event.timestamp_micros) BETWEEN '2017-04-01 12:00:00' AND '2017-06-01 16:00:00'
但我只需要没有约会的时间。有没有办法只按时间查询?
答案 0 :(得分:2)
在BigQuery Standard SQL中你可以使用
EXTRACT(HOUR FROM TIMESTAMP_MICROS(event.timestamp_micros))
以下是用法示例
#standardSQL
SELECT
CASE
WHEN hr >= 4 AND hr < 12 THEN 'morning'
WHEN hr >= 12 AND hr < 15 THEN 'afternoon'
WHEN hr >= 15 AND hr < 17 THEN 'late_afternoon'
WHEN hr >= 17 AND hr < 19 THEN 'evening'
WHEN hr >= 19 AND hr < 21 THEN 'late_evening'
ELSE 'night'
END AS day_part
FROM `dataset.yourtable`,
UNNEST([EXTRACT(HOUR FROM TIMESTAMP_MICROS(event.timestamp_micros))]) AS hr
答案 1 :(得分:1)
有两种根本不同的方法。一种方法是将值转换为时间戳,然后使用时间戳 - 例如将值格式化为小时。
另一个是使用微秒。你可以用除法和模数运算做你想做的事。所以,要计算一天中的小时:
where mod(cast(event.timestamp_micros / (1000000 * 60 * 60) as int64), 24) between 12 and 13
请注意,这时间从12:00:00到13:59.59.999999。
答案 2 :(得分:1)
BigQuery有一个TIME
type可以使用。例如,
SELECT
COUNTIF(time BETWEEN '04:00:00' AND '12:00:00') AS morning_events,
COUNTIF(time BETWEEN '12:00:00' AND '20:00:00') AS evening_events,
COUNTIF(time < '04:00:00' OR time > '20:00:00') AS night_events
FROM (
SELECT
EXTRACT(TIME FROM TIMESTAMP_MICROS(event.timestamp_micros)) AS time
FROM YourTable
);