如何将此查询转换为 Hive SQL?

Posted

技术标签:

【中文标题】如何将此查询转换为 Hive SQL?【英文标题】:How to convert this Query to HiveSQL? 【发布时间】:2020-01-26 21:30:58 【问题描述】:

在 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: mismatched input 'from' 期待(第 4 行,位置 0)

【问题讨论】:

你得到什么错误?也许用错误消息更新问题,也可能是相关的表定义。 您在上面的语句中删除了 oair_d 并且您在查询中引用了该语句>>>> 参考来自oair_db 表定义真的很有帮助!所涉及表的SHOW CREATE TABLE &lt;table_name&gt; 的输出。 这是在 HiveSQL 中运行的正确查询吗? 【参考方案1】:

答案如下:

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

【讨论】:

以上是关于如何将此查询转换为 Hive SQL?的主要内容,如果未能解决你的问题,请参考以下文章

如何将此 SQL 查询转换为 SQLAlchemy

如何将此 sql 查询转换为 mongodb?

如何将此 SQL 查询转换为 NSFetchRequest

如何将此 SQL 内部联接查询转换为 LINQ 语法?

如何将此 SQL 查询转换为 Django?

如何将此 SQL 转换为 Laravel Query Builder 查询?