简体   繁体   中英

hive left outer join with between condition

I know we can't do un-equal joins in hive. I need to convert below query in to hive (hql). Any suggestions/workaround's would be appreciated.

Both table_A and table_B dose not have primary key.

SELECT * 
FROM table_A f
LEFT OUTER JOIN table_B dom1
 ON dom1.country = f.issuing_office_country
AND dom1.ulr_source = 'Loss'
AND dom1.valuation_class = f.dsp_level_join
AND dom1.year_type = 'UW'
AND f.undwrtr_yr_prd_fy_mnth BETWEEN dom1.start_year_month AND dom1.end_year_month

LEFT OUTER JOIN table_B dom2
 ON dom2.country = f.issuing_office_country
AND dom2.ulr_source = 'Short'
AND dom2.valuation_class = f.div_level_join 
AND dom2.year_type = 'UW'
AND f.undwrtr_yr_prd_fy_mnth BETWEEN dom2.start_year_month AND dom2.end_year_month

I think you can fix this (to a close approximation) by using a where clause. It should read:

where (f.undwrtr_yr_prd_fy_mnth BETWEEN dom1.start_year_month AND dom1.end_year_month or
       dom1.valuation_class is null
      ) and
      (f.undwrtr_yr_prd_fy_mnth BETWEEN dom2.start_year_month AND dom2.end_year_month or
       dom2.valuation_class is null
      )

There are some subtle differences between having the condition in the where clause versus the on clause, but they probably will not affect your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM