在Hive JOIN中遇到左右别名;没有任何不等式条款

时间:2016-10-04 12:26:29

标签: sql hive hiveql

我正在使用以下查询:

Select
   S.MDSE_ITEM_I,
   S.CO_LOC_I,
   MAX(S.SLS_D) as MAX_SLS_D,
   MIN(S.SLS_D) as MIN_SLS_D,
   sum(S.SLS_UNIT_Q) as SLS_UNIT_Q,
   MIN(PRSMN_VAL_STRT_D) as PRSMN_VAL_STRT_D,
   MIN(PRSMN_VAL_END_D) as PRSMN_VAL_END_D,
   MIN(RC.FRST_RCPT_D) as FRST_RCPT_D,
   MIN(RC.CURR_ACTV_FRST_OH_D) as CURR_ACTV_FRST_OH_D,
   MIN(H.GREG_D) as  OH_GREG_D  
from
   eefe_lstr4.SLS_TBL as S  
left outer join
   eefe_lstr4.PRS_TBL P 
      on S.MDSE_ITEM_I = P.MDSE_ITEM_I 
      and S.CO_LOC_I = P.CO_LOC_I 
      and S.SLS_D between PRSMN_VAL_STRT_D and PRSMN_VAL_END_D  
left outer join
   eefe_lstr4.OROW_RCPT RC 
      on RC.MDSE_ITEM_I =S.MDSE_ITEM_I 
      and RC.CO_LOC_I =  S.CO_LOC_I  
left outer join
   eefe_lstr4.OH H 
      on H.MDSE_ITEM_I =S.MDSE_ITEM_I 
      and H.CO_LOC_I = S.CO_LOC_I  
group by
   S.MDSE_ITEM_I,
   S.CO_LOC_I;

我收到错误说:

  

FAILED:SemanticException第0行:-1左右别名   在JOIN'PRSMN_VAL_END_D'

中遇到

搜索显示在查询中包含不等式子句时会出现此错误。但是,我没有在我的查询中使用任何不等式子句{<=>=(仅=between),即使这样我也收到此错误。

3 个答案:

答案 0 :(得分:17)

尝试将不等式条件从on子句移动到where条件。

Select S.MDSE_ITEM_I,S.CO_LOC_I,
       MAX(S.SLS_D) as MAX_SLS_D,
       MIN(S.SLS_D) as MIN_SLS_D,
       sum(S.SLS_UNIT_Q) as SLS_UNIT_Q,
       MIN(PRSMN_VAL_STRT_D) as PRSMN_VAL_STRT_D,
       MIN(PRSMN_VAL_END_D) as PRSMN_VAL_END_D,
       MIN(RC.FRST_RCPT_D) as FRST_RCPT_D,
       MIN(RC.CURR_ACTV_FRST_OH_D) as CURR_ACTV_FRST_OH_D,
       MIN(H.GREG_D) as  OH_GREG_D
from eefe_lstr4.SLS_TBL as S
         left outer join eefe_lstr4.PRS_TBL P on S.MDSE_ITEM_I = P.MDSE_ITEM_I and S.CO_LOC_I = P.CO_LOC_I 
         left outer join eefe_lstr4.OROW_RCPT RC on RC.MDSE_ITEM_I =S.MDSE_ITEM_I and RC.CO_LOC_I =  S.CO_LOC_I
         left outer join eefe_lstr4.OH H on H.MDSE_ITEM_I =S.MDSE_ITEM_I and H.CO_LOC_I = S.CO_LOC_I
where(S.SLS_D between PRSMN_VAL_STRT_D and PRSMN_VAL_END_D)
group by S.MDSE_ITEM_I, S.CO_LOC_I;

答案 1 :(得分:9)

我用这种方法看到的问题是,因为有一个left outer join,这意味着我们希望只有一次来自左表的所有寄存器,如果我们将条件移到where子句,然后右表列 null的那些注册表将丢失。

答案 2 :(得分:2)

你是对的。 where子句应该包含可以删除记录的空值:

其中(PRSMN_VAL_STRT_D为空)或(PRSMN_VAL_STRT_D与PRSMN_VAL_END_D之间的S.SLS_D)