对配置单元中左外联接的非相等联接的sql查询

izkcnapc  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(461)

我正在尝试在两个具有非等联接条件的表之间进行左外联接,但配置单元不支持它。在where子句中添加条件会导致数据丢失。如果有人有解决办法,请告诉我。下面是示例代码snipset

Select B.dt ,D.field, sum(B.qty)
from A INNER join B ON A.dt= B.dt
INNER Join C ON B.nbr=C.nbr
LEFT OUTER JOIN D ON A.nbr2=D.Nbr2
AND B.nbr=D.nbr
---Below non equi join not supported
AND B.dt between C.start_date and C.End_Date 
-- Need suggestion of this non equi join.

以下是配置单元中非equi联接的错误:failed:semanticexception[error 10017]:行9:4联接“start\u date”中同时遇到左别名和右别名

niknxzdl

niknxzdl1#

在你的情况下,有一种方法可以做到这一点。那是一个 union all /窗口函数方法。我想这正是你想要的:

with t as (
      select a.nbr2, b.nbr, b.dt, null as end_date, null as field, b.qty
      from A join
           B
           on A.dt = B.dt
      union all
      select d.nrb2, d.nbr, d.start_date, d.end_date, d.field, null
      from D
    )
select dt, (case when dt < d_end_date then d_field end), sum(qty)
from (select t.*, 
             last_value(field, true) over (partition by nbr, nbr2 order by dt) as d_field,
             last_value(end_date, true) over (partition by nbr, nbr2 order by dt) as d_end_date
      from t
     ) t
group by dt, dt, (case when dt < d_end_date then d_field end);

我不是100%确定这是完全相同的——例如,这假设d中最多有一个匹配的记录,并且没有重叠。但其思想是将这些值交错并使用窗口函数作为 last_value()ignore null s选项来获取正确的值。

相关问题