如何将SQL查询转换为HiveSQL并获取最小日期,而不是如下所示使用datepart:
%sql
-- To clear table if it already exists
DROP TABLE IF EXISTS bar;
-- Create temp table syntax
CREATE TEMP VIEW bar AS
--// Start date containing information about year and quarter
SELECT
min(cast (datepart(year, startdate)||datepart(quarter, startdate) as bigint)) as st_dte,
max(cast (datepart(year, enddate)||datepart (quarter, enddate) as bigint)) as end_dte, a_id, carr as bar_code,
case when wac < 100
then 'Dom Flg bar'
else 'Int Flg bar' end as bar_flag,
case when a_id in (343, 455, 123, 656, 645)
then 1
else 0
end as lcc_bar
from oair_cardecode
GROUP BY a_id, bar_code, bar_flag, lcc_bar;
-此代码在数据块中返回错误。
对于Hive> = 1.3.0使用quarter(date)
函数,对于Hive <1.3.0使用ceil(month(date) / 3.0)
作为四分之一
select
min(cast(concat(year(startdate),quarter(startdate)) as bigint)) as st_dte,
max(cast(concat(year(enddate),quarter(enddate)) as bigint)) as end_dte,
a_id,
carr as bar_code,
case when wac < 100 then 'Dom Flg bar' else 'Int Flg bar' end as bar_flag,
case when a_id in (343, 455, 123, 656, 645) then 1 else 0 end as lcc_bar
from oair_cardecode
group by a_id, carr,
case when wac < 100 then 'Dom Flg bar' else 'Int Flg bar' end,
case when a_id in (343, 455, 123, 656, 645) then 1 else 0 end;
如何转换此值:
%sql
-- To clear table if it already exists
DROP TABLE IF EXISTS oair_d;
-- Create temp table syntax
CREATE TEMP VIEW oair_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 oair_db1bmarket
LEFT OUTER JOIN aptcty as t1 on oair_db1bmarket.originairportid=t1.airportid
LEFT OUTER JOIN aptcty as t2 on oair_db1bmarket.destairportid=t2.airportid
LEFT OUTER JOIN #car_airlineid on oair_db1bmarket.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;