我有一个查询,其中要根据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
我需要转换查询,以便它可以根据给定时区的天边界汇总数据。该查询应返回一个有序的序列,该列的一列表示相对于给定时区和提供的日期范围的天数。为了澄清,我需要数据采用以下形式:
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'
因此,第一天的status
将在2019-02-28T00:00:00+11:00
和2019-03-01T00:00:00+11:00
之间聚集
假设我可以将offset
作为参数传递给查询,并且效率不是一个重要的考虑因素(我正在寻找一种自我解决方案的快速解决方案,包含的查询),如何执行分组并返回日期编号?
BigQuery似乎没有convert
函数,因此我似乎无法在group by
中使用类似功能:
group by convert(sample_date_time, dateadd(hours, offset, sample_date_time))
对于我为实现该目标应该考虑的任何建议,我们深表感谢。
答案 0 :(得分:2)
我将使用时区转换数据库中的日期。就个人而言,我经常这样做:
select date(sample_date_time, 'America/New_York') as dte, count(*)
from t
group by dte;
这仅是示例。您的查询显然更复杂。