如何计算在hive中不包括假期的工作日数?

zkure5ic  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(317)

我能够在mysql中找到两个日期之间的差异,但是相同的查询在hive中不起作用。
我有一张table,上面有假日的日期表。

SELECT bd.*,
       (SELECT COUNT(*) FROM holiday WHERE h_date BETWEEN bd.t_date AND bd.start_date) as holiday_count
FROM   biz_date as bd

配置单元中出错:

Error: Error while compiling statement: FAILED: ParseException line 2:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification (state=42000,code=40000)
laik7k3q

laik7k3q1#

这就是你要找的吗?

SELECT t_date,start_date, holiday_count from biz_date as bd
INNER JOIN (SELECT COUNT(*) as holiday_count FROM holiday,biz_date as bd WHERE h_date BETWEEN bd.t_date AND bd.start_date) as holiday_count
ON 1=1

输出:

+-------------+-------------+----------------+--+
|   t_date    | start_date  | holiday_count  |
+-------------+-------------+----------------+--+
| 2018-12-31  | 2019-02-01  | 2              |
+-------------+-------------+----------------+--+

完整测试代码:

create table temp.biz_date (                                                                                                                                                                                     
t_date                  string
,start_date)
stored as parquet location '/temp.db/biz_date' tblproperties("parquet.compression=SNAPPY";

create table temp.biz_date(
t_date                   CHAR(10) 
, start_date             CHAR(10) )
stored as parquet location '/temp.db/biz_date' tblproperties("parquet.compression=SNAPPY") ;

INSERT INTO TABLE temp.holiday VALUES ('2019-01-01'),('2019-01-05');
INSERT INTO TABLE temp.biz_date  VALUES ('2018-12-31','2019-02-01');

相关问题