【问题标题】:datetime query of particular hours for entire year全年特定时间的日期时间查询
【发布时间】:2022-01-17 10:25:56
【问题描述】:

<table><tbody><tr><th>Row</th><th>Identifier</th><th>date_time</th><th>HvacMode</th><th>CalendarEvent</th></tr><tr><td>1</td><td>a578bb21941003e7a58a399375b20f274fab5233</td><td>2020-10-31 14:50:00 UTC</td><td>heat</td><td> </td></tr><tr><td>2</td><td>683227685eb44f9d231d511e8ae5255ecc1b71fb</td><td>2019-08-19 02:00:00 UTC</td><td>cool</td><td> </td></tr><tr><td>3</td><td>1afd0d7a8408458d39dde687e1218f2948907c4b</td><td>2021-07-19 19:40:00 UTC</td><td>auto</td><td> </td></tr><tr><td>4</td><td>607db2c120782353da1539a008956301341d54c9</td><td>2020-08-29 16:50:00 UTC</td><td>cool</td><td> </td></tr><tr><td> </td><td> </td><td> </td><td> </td><td> </td></tr></tbody></table>

我想获取一年中每天早上 7 点到晚上 7 点(7:00-19:00)的特定时间段的数据。我有一个格式为 2017:01:01 00:00:00 的日期时间字段(示例)。这里有点新手。我知道如何提取特定日期范围,但我不知道如何提取特定时间的日期范围。在 BigQuery 中处理非常大的数据集。任何帮助都会有所帮助。 sample data image

【问题讨论】:

  • 不清楚您需要什么。您能否提供您拥有的数据样本和预期结果?!
  • 我无法让表格格式正常工作,所以我在上面的问题中添加了一张图片。我只想要从 07:00:00 到 19:00:00 的数据

标签: sql datetime google-bigquery


【解决方案1】:

如果我理解您的问题,您正在尝试按 7-7 时间段过滤每天的数据。 试试下面的例子:

with sample_data as (
    SELECT ts FROM UNNEST(generate_timestamp_array('2021-01-01 00:00:00', '2021-03-01 00:00:00', INTERVAL 1 hour)) as ts
)

select *
from sample_data
where 
extract(year from ts) = 2021
and extract(hour from ts) between 7 and 19;

使用提取函数,您可以从时间戳中提取小时,然后在中间进行过滤。有关提取功能的更多信息,请参阅以下文档: https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#extract

【讨论】:

  • 最好在sql中创建一次日期维度并查询。
  • 我不确定我是否理解您的评论。如果您指的是 CTE,它是一组示例数据,用于在隔离环境中测试功能。根据问题,实际解决方案中应该不需要日期暗淡。
【解决方案2】:

全部;

这适用于我需要的数据。谢谢您的帮助。下面的 sn-p 为我提供了一月份的白天时间。

select *
from sample_data where extract(hour from date_time) between 7 and 19
AND date_time between '2017-01-01 00:00:00' and '2017-01-31 23:59:00';

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 2011-12-29
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多