按日期对UTC日期进行分组和汇总,调整为指定的时区偏移量

问题描述 投票:0回答:2

我有一个查询,我在确定特定日期的整体status,基于在BigQuery中按UTC日期聚合数据,以便生成的数据将具有以下形式:

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

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

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作为参数传递给查询,并且效率不是一个重要的考虑因素(我正在寻找自包含查询中的快速解决方案),我该如何执行分组,并返回天数?

BigQuery似乎没有convert函数,所以我似乎无法在我的group by中使用这样的东西:

group by convert(sample_date_time, dateadd(hours, offset, sample_date_time))

对于我应该看到什么来实现这一点的任何建议表示赞赏。

sql google-bigquery aggregate timezone-offset
2个回答
2
投票

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

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

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


1
投票

感谢@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时区,例如'澳大利亚/维多利亚' - 见https://cloud.google.com/dataprep/docs/html/Supported-Time-Zone-Values_66194188

© www.soinside.com 2019 - 2024. All rights reserved.