配置单元嵌套查询左联接

时间:2020-04-15 11:03:40

标签: hive hiveql hive-query

我是蜂巢的新手,我想实现以下查询

   select a.controlid, 
          b.name as campaign, 
          a.controlactivityid as activitysource,
          c.code as  codemyaprc,
          c.label_en as label_en,
          c.label_fr as label_fr
   from bo_h_control.bridgeactivity a 
        join suvh_econtrol.TMP_GPS_REF b ON a.controlcampaignid = 
   b.ps_cam_id
        left join  srv_h_a8460_fpc.activity c 
                   ON a.controlactivityid=c.code 
                      and c.date_creation= select 
   max(date_creation) from srv_h_a8460_fpc.activity

直到最后一个左联接,它都可以正常工作。巢状查询显然无法在蜂巢中获得授权。

我该如何处理?

1 个答案:

答案 0 :(得分:1)

使用子查询代替联接表:

 select a.controlid, 
          b.name as campaign, 
          a.controlactivityid as activitysource,
          c.code as  codemyaprc,
          c.label_en as label_en,
          c.label_fr as label_fr
   from bo_h_control.bridgeactivity a 
        join suvh_econtrol.TMP_GPS_REF b ON a.controlcampaignid = 
   b.ps_cam_id
        left join (select c.*, max(date_creation) over() as max_date_creation
                     from srv_h_a8460_fpc.activity c 
                  ) c ON a.controlactivityid=c.code 
                         and c.date_creation=c.max_date_creation

或者最好在子查询的WHERE子句中移动最后一个联接条件:

 select a.controlid, 
          b.name as campaign, 
          a.controlactivityid as activitysource,
          c.code as  codemyaprc,
          c.label_en as label_en,
          c.label_fr as label_fr
   from bo_h_control.bridgeactivity a 
        join suvh_econtrol.TMP_GPS_REF b ON a.controlcampaignid = 
   b.ps_cam_id
        left join (select * from
                   (
                   select c.*, max(date_creation) over() as max_date_creation
                     from srv_h_a8460_fpc.activity c 
                   )c where max_date_creation=date_creation
                  ) c ON a.controlactivityid=c.code