使用数组确定BigQuery / Standard SQL中给定日期的状态

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

对于日历年的每一天,我需要检查记录的状态,并根据状态将其标记为1或0(已发布= 1,否则为0)。状态位于附加到每条记录的数组中。

我将记录和数组加入到表中,其中包含一个日历,其中包含给定年份每一天的日期。

`select
cdate,
YYYYMM,
_id,
createdat,
case when cdate>=statushistory.date and <statushistory.date and statushistory.status="Published" then 1 else 0 end as active,
statushistory
from bigquery.calendar 
join rs.listings on extract(date from createdat)<=cdate, unnest(statushistory)
where _id ="HGk5HMd6ZxmSRgEJ6"
;`

我被卡住了。我无法参考statushistory中的特定字段:statushistory.statusstatushistory.date。当我尝试引用它们时,它给出了它们在数组中的错误。

即使我知道如何引用它们,我需要将cdate置于正确的两个statushistory.date之间以确定状态。

上面引用的_id有4个状态和4个日期。

如果在cdate期间状态为“已发布”,我的目标是创建1

enter image description here

sql arrays google-bigquery
1个回答
1
投票

我对你想要做什么有点不清楚。但是,我怀疑它与获取每个日期的状态有关。

如果是这样,你可以在子查询中unnest

select c.cdate, c.YYYYMM, l._id, l.createdat,
       (select sh.status
        from unnest(l.statushistory) sh
        where c.cdate >= sh.date and 
              c.cdate <= sh.date
       ) as status_on_date
from bigquery.calendar c join
     rs.listings l
     on extract(date from l.createdat)<= c.cdate 
where l._id = 'HGk5HMd6ZxmSRgEJ6'
© www.soinside.com 2019 - 2024. All rights reserved.