Hive:根据一系列时间过滤行

时间:2017-09-25 16:33:17

标签: hive

我是这个论坛的新手,这是我的第一个问题。我确实在这个论坛上搜索解决方案,但仍无法得到确切的解决方案。如果这样的问题已经得到解答,那么我提前道歉。

现在回到我的问题。我有一张HIVE表" table1"有以下列:

channel_name     string
start_time       string
prog_name        string
cost             double

以下是上表中的样本日期:

BBC      2016-11-24 05:47:02     NEW: Wonder World       191.0
GTV      2016-11-24 21:35:58     NEW: Great Escape       99.0
BBC      2016-11-25 21:43:29     NEW: Wonder World       131.0
GTV      2016-11-25 23:32:56     NEW: STATE OF FEAR      145.0
GTV      2016-11-26 01:30:30     NEW: Great Escape       128.0

我必须根据UI上的过滤器找出成本,用户可以根据程序名称和0-5,6-10,11-15 ... 19-24等桶的时间范围进行过滤。我开发了以下HIVE查询应根据程序和用户过滤的时间段选择每一行。我必须比较整个时间戳值的时间部分,而不管日期。

下面是我尝试的HIVE查询,但它无法比较并给出错误:

SELECT sum(cost)
  FROM table1
  WHERE prog_name='NEW: Wonder World'
  AND (cast(substr(start_time, 12) AS TIMESTAMP) BETWEEN (00:00:00
                                                          AND 05:59:59)
       OR cast(substr(start_time, 12) AS TIMESTAMP) BETWEEN (06:00:00
                                                             AND 09:59:59));
  

失败:ParseException第1:118行无法识别'附近的输入:' ' 00'   ':'在表达式规范

转换和比较时间部分与时间戳值的正确语法是什么?感谢上述查询的任何建议/指针。

1 个答案:

答案 0 :(得分:0)

select  sum(cost)   as sum_cost

from    table1 

where   prog_name='NEW: Wonder World' 
    and substr(start_time,12) between '00:00:00' and '09:59:59'
;
+-----------+
| sum_cost  |
+-----------+
| 191       |
+-----------+