如何将此查询转换为hivesql?

hivapdat  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(337)

在databricks中运行此查询时出错。如何将以下sql查询转换为hivesql:

%sql 
-- To clear table if it already exists
DROP TABLE IF EXISTS air_d;

-- Create temp table syntax
CREATE TEMP VIEW  air_d AS

select *,airport_1+'-'+airport_2 as route,cast(citymarketid_1 as varchar)+cast(citymarketid_2 as varchar) as city_route 

from 
(select year, quarter, tkcarrier, nonstopmiles, OriginCityMarketID, DestCityMarketID, Origin, Dest, passengers, mktfare, itingeotype, bulkfare, mktmilesflown, #car_airlineid.car_flag, car_airlineid.airlineid, 
case when t1.citymarketnmesrt < t2.citymarketnmesrt then origin else dest end as airport_1, 
case when t1.citymarketnmesrt < t2.citymarketnmesrt then dest else origin end as airport_2, 
case when t1.citymarketnmesrt < t2.citymarketnmesrt then origincitymarketid else destcitymarketid end as citymarketid_1, 
case when t1.citymarketnmesrt < t2.citymarketnmesrt then destcitymarketid else origincitymarketid end as citymarketid_2 
from air_db 
LEFT OUTER JOIN aptcty  as t1 on air_db.originairportid=t1.airportid 
LEFT OUTER JOIN aptcty  as t2 on air_db.destairportid=t2.airportid 
LEFT OUTER JOIN  #car_airlineid on air_db.tkcarrierairlineid=#car_airlineid.airlineid 
where (Year>=2003 and Year<=2018) or (Year=2019 and Quarter in (1,2)) and OriginCountry = 'US' and DestCountry = 'US') as new_table;

这是错误:
sql语句中出错:parseexception:应为不匹配的输入“from”(第4行,位置0)

wvt8vs2t

wvt8vs2t1#

答案如下:

%sql
create table air_d from (
select *,airport_1 +'-'+ airport_2 as route, string(citymarketid_1) + string(citymarketid_2) as city_route from
(
select year, quarter, tkcarrier, nonstopmiles, OriginCityMarketID, DestCityMarketID, Origin, Dest, passengers, mktfare, itingeotype, bulkfare, mktmilesflown, t3.car_flag, t3.airlineid,
case when t1.citymarketnmesrt < t2.citymarketnmesrt then origin else dest end as airport_1,
case when t1.citymarketnmesrt < t2.citymarketnmesrt then dest else origin end as airport_2,
case when t1.citymarketnmesrt < t2.citymarketnmesrt then origincitymarketid else destcitymarketid end as citymarketid_1,
case when t1.citymarketnmesrt < t2.citymarketnmesrt then destcitymarketid else origincitymarketid end as citymarketid_2
from (
(select * from air_db  ) t0
left join (select * from aptcty ) t1 on t0.originairportid=t1.airportid
left join (select * from aptcty ) t2 on t0.destairportid=t2.airportid
left join (select * from car_airlineid) t3 on t0.tkcarrierairlineid=t3.airlineid )
where ( (Year>=2003 and Year<=2018) or (Year=2019 and Quarter in (1,2))) and OriginCountry = 'US' and DestCountry = 'US'
))

相关问题