我想使用CASE WHEN,LIKE和正则表达式在hive表中编写查询。我使用过regexp
和rlike
,但我没有得到预期的结果。到目前为止,我的尝试如下
select distinct ending from
(select date, ending, name, count(distinct id)
from (select CONCAT_WS("/",year,month,day,hour) as date, id, name,
case when type = 'TRAN' then 'tran'
when events regexp '%[:]no_reply[:]%[^o][^n][:]incomplete[:]%' and type rlike '%HUP' then 'con'
when events not regexp '%[:]no_reply[:]%[^o][^n][:]incomplete[:]%' and type rlike '%HUP' then 'aban'
else 'other'
end as ending
from data_struct1) tmp
group by date, ending, name) tmp2;
并且
select distinct ending from
(select date, ending, name, count(distinct id)
from (select CONCAT_WS("/",year,month,day,hour) as date, id, name,
case when type = 'TRAN' then 'tran'
when events rlike '%[:]no_reply[:]%[^o][^n][:]incomplete[:]%' and type rlike '%HUP' then 'con'
when events not rlike '%[:]no_reply[:]%[^o][^n][:]incomplete[:]%' and type rlike '%HUP' then 'aban'
else 'other'
end as ending
from data_struct1) tmp
group by date, ending, name) tmp2;
两个查询都返回不正确的结果(语法不错,只是结果不正确)。
关于正则表达式量词的文档很多,例如:https://docs.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions
select 'opencase_2,initial_state:inquiry,inquiry:no_reply:initial_state:incomplete::,inquiry:reask:secondary_state:complete::' regexp 'no_reply:[^:]+:incomplete';
OK
true
这也错了:rlike '%HUP'
。它应该像这个'.*HUP$'
(在字符串的末尾)或简单地'HUP'如果HUP所在的位置无关紧要:在字符串的中间或结尾或开头
rlike
和regexp
在你的查询中工作相同,最好使用相同的运算符:regexp或rlike only。这两个是同义词。