如何将此查询转换为HiveSQL?

时间:2020-01-26 21:30:58

标签: sql hive hiveql pyspark-sql databricks

在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)

1 个答案:

答案 0 :(得分:0)

这是答案:

%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'
))