【问题标题】:How to convert SQL Query to HiveSQL and get Min Date?如何将 SQL 查询转换为 HiveSQL 并获取最小日期?
【发布时间】: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;

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

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。

标签: sql hiveql pyspark-sql databricks datepart


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2019-03-31
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多