Как конвертировать этот запрос в Hive SQL? - PullRequest
0 голосов
/ 27 января 2020

Я получаю ошибку при выполнении этого запроса в Databricks. Как преобразовать следующий SQL Запрос в Hive SQL:

%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 голосов
/ 01 февраля 2020

Вот ответ:

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