如何将 SQL 查询转换为 HiveSQL 并获取最小日期?

Posted

技术标签:

【中文标题】如何将 SQL 查询转换为 HiveSQL 并获取最小日期?【英文标题】:How to convert SQL Query to HiveSQL and get Min Date? 【发布时间】:2020-01-25 21:34:55 【问题描述】:

如何将 SQL Query 转换为 HiveSQL 并获取 Min Date 而不是使用 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;

--此代码在数据块中返回错误。

【问题讨论】:

样本数据和期望的结果真的很有帮助。 【参考方案1】:

对于 Hive >= 1.3.0 使用 quarter(date) 函数,对于 Hive 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 查询转换为 HiveSQL 并获取最小日期?的主要内容,如果未能解决你的问题,请参考以下文章

hive sql的语法帮助在哪

如何将命名查询转换为 sql 查询?

转载Hive SQL

SQLHive SQL详解

hive sql 语法详解

如何查看hivesql里执行了哪些查询