根据 UTC 日期按天分组和汇总,调整为指定的时区偏移量

Group and aggregate by day against UTC dates, adjusted to a specified timezone offset

我有一个查询,我根据 BigQuery 中的 UTC 日期聚合数据来确定特定日期的总体 status,因此生成的数据将具有以下形式:

date            status
----            ------
28-feb-2019     0
01-mar-2019     1

这是查询,其中 sample_date_time 是 BigQuery 中的 UTC 日期。 @startDateTime@endDateTime 当前作为 UTC 日期传递,始终表示 UTC 日期边界,例如

@startDateTime = '2019-02-28T00:00:00.000Z'

@endDateTime = '2019-03-01T00:00:00.000Z'

select CAST(sample_date_time AS DATE) as date,
       (case when sum(case when status_code >> 0 = 0 then 1 else 0 end) > 0 
             then 0 
        else 
             case when sum(case when status_code >> 0 = 1 then 1 else 0 end) = 1
             then 1
             end
        end) as status 
from (
  with data as
    (
      select
        sample_date_time,
        status_code
      from `my.table` 
      where sample_date_time between @startDateTime and @endDateTime
      order by sample_date_time
    )

  select sample_date_time, status_code
  from data
)
group by date
order by date

我需要转换我的查询,以便它可以根据给定时区的日期边界聚合数据。查询应该 return 一个有序序列,其中有一列表示相对于给定时区和提供的日期范围的天数。为了澄清,我需要数据采用以下形式:

day            status
----           ------
1              0
2              1

@startDateTime@endDateTime 将作为 ISO_8601 日期传递,这些日期始终表示给定时区中的日期边界,并且采用提供时区偏移相对的格式到 UTC 例如:

@startDateTime = '2019-02-28T00:00:00+11:00'

@endDateTime = '2019-03-01T00:00:00+11:00'

因此,第 1 天的 status 将在 2019-02-28T00:00:00+11:002019-03-01T00:00:00+11:00

之间汇总

假设我可以将 offset 作为参数传递到查询中,并且 效率不是重要的考虑因素(我正在寻找一个快速的自包含查询中的解决方案),如何执行分组,以及 return 天数?

BigQuery 似乎没有 convert 函数,所以我似乎无法在我的 group by:

中使用类似的东西
group by convert(sample_date_time, dateadd(hours, offset, sample_date_time))

任何关于我应该如何实现这一目标的建议都将受到赞赏。

我会使用时区转换数据库中的日期。就个人而言,我经常这样做:

select date(sample_date_time, 'America/New_York') as dte, count(*)
from t
group by dte;

这只是一个例子。您的查询显然更复杂。

感谢@Gordon Linoff 提供了简单、优雅的解决方案,它允许我以这种形式保存数据,但日期转换为相对于所需时区的日期,即:

date (in specified TZ)    status
----------------------    ------
28-feb-2019               0
01-mar-2019               1

这是我的最终查询。它基于将 time_zone 作为我的数据中的列提供。它还依赖于本地化时间表达式中提供的开始和结束日期时间范围,使用以下 ISO8601 格式:

`yyyy-mm-ddThh:mm:ss+hh:mm`

(最后的 +hh:mm 表示已应用于初始日期时间表达式的时区相对偏移量,即 yyyy-mm-ddThh:mm

select date(localised_sample_date_time) as localised_date,
       (case when sum(case when status_code >> 0 = 0 then 1 else 0 end) > 0 
             then 0 
        else 
             case when sum(case when status_code >> 0 = 1 then 1 else 0 end) = 1
             then 1
             end
        end) as status 
from (
  with data as
    (
      select
        DATETIME(sample_date_time,time_zone)as localised_sample_date_time,
        status_code
      from `my.table` 
      where sample_date_time between '2019-03-01T00:00:00.000+1:00' and '2019-03-02T23:59:59.000+1:00' -- get data for the the 1st March (relative to Central European Standard Time i.e. UTC+1)
      order by sample_date_time
    )

  select localised_sample_date_time, status_code
  from data
)
group by localised_date
order by localised_date

time_zone = 有效的 BigQuery 时区,例如'Australia/Victoria' - 见 https://cloud.google.com/dataprep/docs/html/Supported-Time-Zone-Values_66194188