2016-03-15 39 views
1

我有這個有效的T-SQL查詢:蜂巢SQL:左,右別名遇到JOIN

select t1.* 
    ,case when s1.period is not null then 'Y' else 'N' end as flag_cur 
    ,case when s2.period is not null then 'Y' else 'N' end as flag_prev 
    ,s1.cutoff_date as cutoff_date_cur ,s1.cutoff_dtkey as cutoff_dtkey_cur 
    ,s2.cutoff_date as cutoff_date_prev ,s2.cutoff_dtkey as cutoff_dtkey_prev 
into #tmp_leads2 
from #tmp_leads t1 
left join #param s1 on s1.period = '(a) Current' and s1.begin_date <= t1.CreatedDate and t1.CreatedDate < s1.end_date 
left join #param s2 on s2.period = '(b) Previous' and s2.begin_date <= t1.CreatedDate and t1.CreatedDate < s2.end_date 

我試圖重新寫吧蜂巢(V0.13)爲:

create table tmp_leads2 as 
select t1.* 
    ,case when s1.period is not null then 'Y' else 'N' end as flag_cur 
    ,case when s2.period is not null then 'Y' else 'N' end as flag_prev 
    ,s1.cutoff_date as cutoff_date_cur ,s1.cutoff_dtkey as cutoff_dtkey_cur 
    ,s2.cutoff_date as cutoff_date_prev ,s2.cutoff_dtkey as cutoff_dtkey_prev 
from tmp_leads t1 
left join param s1 on s1.period = '(a) Current' and s1.begin_date <= t1.CreatedDate and t1.CreatedDate < s1.end_date 
left join param s2 on s2.period = '(b) Previous' and s2.begin_date <= t1.CreatedDate and t1.CreatedDate < s2.end_date ; 

但我得到的錯誤:

Error occurred executing hive query: OK FAILED: SemanticException [Error 10017]: Line 8:53 Both left and right aliases encountered in JOIN 'CreatedDate' 

我看到它在談論的領域,但我不敢肯定,同時保持查詢結果如何重新寫s相同。

回答

3

問題來自join中的不等式條件。這提出了一個問題。以下可能足以滿足您的需求:

create table tmp_leads2 as 
    select t1.*, 
      (case when s1.period is not null then 'Y' else 'N' end) as flag_cur, 
      (case when s2.period is not null then 'Y' else 'N' end) as flag_prev, 
      s1.cutoff_date as cutoff_date_cur, s1.cutoff_dtkey as cutoff_dtkey_cur , 
      s2.cutoff_date as cutoff_date_prev, s2.cutoff_dtkey as cutoff_dtkey_prev 
    from tmp_leads t1 left join 
     param s1 
     on s1.period = '(a) Current' left join 
     param s2 
     on s2.period = '(b) Previous' 
    where (s1.begin_date is null or s1.begin_date <= t1.CreatedDate and t1.CreatedDate < s1.end_date) or 
      (s2.begin_date is null or s2.begin_date <= t1.CreatedDate and t1.CreatedDate < s2.end_date); 

這不完全相同。它假定如果一個參數在表中,那麼它就在所有日期的表中。這可能是一個合理的假設。如果不是,則需要更復雜的查詢。

0

這裏有一些東西,會不會導致內部連接或別名問題,給你在蜂巢

create table tmp_leads2 as 
    select final.* 
     ,case when s1period is not null then 'Y' else 'N' end as flag_cur 
     ,case when s2period is not null then 'Y' else 'N' end as flag_prev 

    from 
    (select t1.*, 
     max(case when s1.begin_date <= t1.CreatedDate and t1.CreatedDate < s1.end_date then s1.peroid else null end) as s1period, 
     max(case when s1.begin_date <= t1.CreatedDate and t1.CreatedDate < s1.end_date then s1.cutoff_date else null end) as cutoff_date_cur, 
     max(case when s1.begin_date <= t1.CreatedDate and t1.CreatedDate < s1.end_date then s1.cutoff_dtkey else null end) as cutoff_dtkey_cur, 

     max(case when s2.begin_date <= t1.CreatedDate and t1.CreatedDate < s2.end_date then s2.peroid else null end) as s2period, 
     max(case when s2.begin_date <= t1.CreatedDate and t1.CreatedDate < s2.end_date then s2.cutoff_date else null end) as cutoff_date_prev, 
     max(case when s2.begin_date <= t1.CreatedDate and t1.CreatedDate < s2.end_date then s2.cutoff_dtkey else null end) as cutoff_dtkey_prev, 

    from tmp_leads t1 
    left join param s1 on s1.period = '(a) Current' 
    left join param s2 on s2.period = '(b) Previous' 
    group by t1.* /* type all column names required from t1*/ 
    ) final ; 
預期的結果