How to implement lookahead in Bigquery

时间:2019-03-19 15:11:35

标签: google-bigquery

I have sample data as

with temp as (
select DATE("2019-01-02") as time_stamp, "3" as id
union all
select DATE("2019-01-03") as time_stamp, "6" as id
union all
select DATE("2019-01-04") as time_stamp, "5" as id
union all
select DATE("2019-01-05") as time_stamp, "11" as id
)
select time_stamp, id
from temp

I want to implement lookahead logic in bigQuery, meaning if todays date is "2019-01-02" then I want all the data for next N days (assume N = 3 for simplicity).

For the above example, it would return IDs 6,5 and 11 if I run on 2019-01-02. (logic :: capture all IDs for next 3 days from 2019-01-02)

I want to implement this for a given date range with lookahead window N=3 days.So if I run it for fromDate=2019-01-02 and toDate=2019-01-03 then I want 2 sets of results. For date=2019-01-02 I will need IDs 6,5,11 and for date=2019-01-03 I will need IDs 5 and 11.

So the output would be

processed_date, id
2019-01-02, 6
2019-01-02, 5
2019-01-02, 11
2019-01-03, 5
2019-01-03, 11

1 个答案:

答案 0 :(得分:1)

我想我已经找到了一种方法,但是我不确定它是否有效。这是我的SQL代码-

with temp as (
select DATE("2019-01-02") as time_stamp, "3" as id
union all
select DATE("2019-01-03") as time_stamp, "6" as id
union all
select DATE("2019-01-04") as time_stamp, "5" as id
union all
select DATE("2019-01-05") as time_stamp, "11" as id
union all
select DATE("2019-01-08") as time_stamp, "13" as id
),
date_ranges as ( select DATE("2019-01-02") as fromDate , DATE("2019-01-03") as toDate , 3 as window_length ) ,
distinct_dates as (
SELECT day , (select window_length as window_length from date_ranges) window_length
FROM UNNEST(
    GENERATE_DATE_ARRAY((select fromDate from date_ranges), (select toDate from date_ranges), INTERVAL 1 DAY)
) AS day
)
-- choose your columns from here
select * , date_diff(time_stamp, day, day) as date_difference 
from temp
cross join distinct_dates 
where date_diff(time_stamp, day, day) > 0 and date_diff(time_stamp, day, day) <= (select window_length as window_length from date_ranges)
order by day