【发布时间】:2019-09-05 01:43:30
【问题描述】:
我在 Google BigQuery 中有如下所示的数据:
sample_date_time_UTC time_zone milliseconds_between_samples
-------- --------- ----------------------------
2019-03-31 01:06:03 UTC Europe/Paris 60000
2019-03-31 01:16:03 UTC Europe/Paris 60000
...
数据样本需要定期进行,由 milliseconds_between_samples 字段的值指示:
time_zone 是一个字符串,代表 Google Cloud Supported Timezone Value
然后,我会检查任何一天范围内的实际样本数量与预期数量的比率(表示为本地日期,对于给定的time_zone):
with data as
(
select
-- convert sample_date_time_UTC to equivalent local datetime for the timezone
DATETIME(sample_date_time_UTC,time_zone) as localised_sample_date_time,
milliseconds_between_samples
from `mytable`
where sample_date_time between '2019-03-31 00:00:00.000000+01:00' and '2019-04-01 00:00:00.000000+02:00'
)
select date(localised_sample_date_time) as localised_date, count(*)/(86400000/avg(milliseconds_between_samples)) as ratio_of_daily_sample_count_to_expected
from data
group by localised_date
order by localised_date
问题在于这有一个错误,因为我已将一天中的预期毫秒数硬编码为86400000。这是不正确的,因为当夏令时在指定的time_zone (Europe/Paris) 开始时,一天缩短了 1 小时。夏令时结束后,白天会延长 1 小时。
所以,上面的查询是不正确的。它在Europe/Paris 时区(即该时区开始夏令时)查询今年 3 月 31 日的数据。当天的毫秒数应该是82800000。
在查询中,如何获取指定localised_date 的正确毫秒数?
更新:
我试着这样做看看它会返回什么:
select DATETIME_DIFF(DATETIME('2019-04-01 00:00:00.000000+02:00', 'Europe/Paris'), DATETIME('2019-03-31 00:00:00.000000+01:00', 'Europe/Paris'), MILLISECOND)
那没用 - 我收到 86400000
【问题讨论】:
标签: datetime google-bigquery dst