您不需要使用相同的列名来连接表
with
transactions as (
select
*
from
unnest(
array[
struct(
'xxx' as pos_id,
1 as transaction_id,
date'2019-06-20' as transaction_date,
200 as amount),
struct('xxx', 2, date'2019-07-30', 1000),
struct('xxx', 3, date'2019-04-10', 500),
struct('xxx', 4, date'2019-05-15', 2000)
]
)
),
cashbacks as (
select
*
from
unnest(
array[
struct(
'xxx' as pos_id,
date'2019-01-01' as start_date,
date'2019-04-30' as end_date,
0.1 as cashback),
struct('xxx', date'2019-07-01', date'2019-09-30', 0.2)
]
)
)
select
t.*,
c.* except(pos_id)
from
transactions as t
left join cashbacks as c
on t.pos_id = c.pos_id
and t.transaction_date between c.start_date
and c.end_date
order by
1, 3 desc