关于日期、小时和分钟的 Where 子句

Where clause on date, hour & minute

我有一个table。

userId|date      |hour|minute|five|four|three|two|one|
------+----------+----+------+----+----+-----+---+---+
dinesh|2021-05-28|   6|     5|   0|   1|    0|  0|  0|
jay   |2021-05-29|   1|    20|   1|   0|    1|  0|  0|

如何在给定范围内过滤日期、小时和分钟。 假设我想要从 2021-05-28 05:00 到 2021-05-29 23:59

的所有条目

我将执行一些聚合查询,例如

select userId, date, sum(five) as five, sum(four) as four
from table where <2021-05-28 05:00 to 2021-05-29 23:59> group by userId, date

考虑下面的 BigQuery 标准 SQL(假设 date 列是日期数据类型)

select userId, date, sum(five) as five, sum(four) as four
from your_table
where datetime(date, time(hour, minute, 0)) 
between datetime('2021-05-28 05:00:00') and datetime('2021-05-29 23:59:59')
group by userId, date

由于您本质上是在存储时间戳并尝试使用它进行时间戳操作,因此我建议使用 timestamp 类型。您的数据将如下所示:

 userid |      timestamp      | five | four | three | two | one
--------+---------------------+------+------+-------+-----+-----
 dinesh | 2021-05-28 06:05:00 |    0 |    1 |     0 |   0 |   0
 jay    | 2021-05-29 01:20:00 |    1 |    0 |     1 |   0 |   0

你可以这样查询:

select userId, timestamp::date, sum(five) as five, sum(four) as four
from your_table
where timestamp between '2021-05-28 05:00:00' and '2021-05-29 23:59:59'
group by userId, timestamp::date