Joining tables that compute values between dates

偶尔善良 提交于 2019-12-02 03:57:57

Hmmm. In standard SQL you can do:

select b.*,
       (select a.num
        from a
        where a.date <= b.date
        order by a.date desc
        fetch first 1 row only
       ) * value as new_value
from b;

I don't think this meets the range conditions, but I don't understand your description of that.

I also don't know if Impala supports correlated subqueries. An alternative is probably faster on complex data:

with ab as (
      select a.date, a.value as a_value, null as b_value, 'a' as which
      from a
      union all
      select b.date, null as a_value, b_value, 'b' as which
      from b
     )
select date, b_value * a_real_value
from (select ab.*,
             max(a_value) over (partition by a_date) as a_real_value
      from (select ab.*,
                   max(a.date) over (order by date, which) as a_date
            from ab
           ) ab
     ) ab
where which = 'b';

This works on MariaDb (MySql) and it's pretty basic so hopefully it works on impala too.

SELECT b.date, b.value * a.num
FROM tableB b, tableA a
WHERE b.date >= a.date
  AND (b.date < (SELECT MIN(c.date) FROM tableA c WHERE c.date > a.date)
       OR NOT EXISTS(SELECT c.date FROM tableA c WHERE c.date > a.date))

The last NOT EXISTS... was needed to include dates after the last date in table A

Update In the revised version of the question the date in B is never larger (after) the last date in A so then the query can be written as

SELECT b.date, b.value * a.num
FROM tableB b, tableA a
WHERE b.date >= a.date
  AND b.date <= (SELECT MIN(c.date) FROM tableA c WHERE c.date > a.date)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!