I'm trying to sum values for specific date range around current row. Since Bigquery do not suppport dated ranges in windows functions, I use self join, like this:
with test_data as (
select 1 val1, 7 val2, 'ord001' id, timestamp('2019-01-01 04:00:00') dt_order
union all
select 2 val1, 14 val2, 'ord002' id, timestamp('2019-01-02 05:00:00') dt_order
union all
select 3 val1, 21 val2, 'ord003' id, timestamp('2019-01-03 06:00:00') dt_order
)
,revenue_coeff as (
select
td.id,
td.val1 *
(select sum(td1.val2) / sum(td1.val1)
from test_data td1
where td1.dt_order >= timestamp_sub(td.dt_order, interval 24 hour) and
td1.dt_order < timestamp_add(td.dt_order, interval 6 minute)
)
from test_data td
)
select * from revenue_coeff
This toy query works just fine. But when I'm trying to use a real BigQuery table, I get a 'LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join' message. How can I implement such a query in BQ? Thanks in advance!
mark accepted answer
by using the tick on the left of the posted answer, below the voting. See meta.stackexchange.com/questions/5234/… for why it is important! Also important tovote on answer
. Vote up answers that are helpful. ... You can check about what to do when someone answers your question - stackoverflow.com/help/someone-answers. Following these simple rules you increase your own reputation score and at the same time you keep us motivated to answer your questions :o) please consider! – Mikhail Berlyant