在Redshift中使用串联从多个字符串中选择一个

时间:2017-07-17 20:04:56

标签: sql amazon-redshift

我必须通过在Amazon Redshift中编写查询来匹配以下网址:

<some url>/www.abc.com/a/<more url>
<some url>/www.abc.com/b/<more url>
<some url>/www.abc.com/c/<more url>
<some url>/www.abc.com/d/<more url>

在这里,显然“/www.abc.com/”是不变的,但'/'后面的文字可以改变。它可以采用我列出的众多值中的一个(在这种情况下为a,b,c,d)。我如何匹配“/www.abc.com /”之后的这个部分?

我能想到以下几点:

select text,
       case 
           when text ilike '%/www.abc.com/' || <what should go here?> || '/%' then 'URLType1'
           when <some other condition> then 'URLType2'
       end as URLType
from table

我必须保持CASE结构。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

选项是:

1)将值列表放入子查询中,然后像这样加入此列表:

with
value_list as (
    select 'a' as val union select 'b' union select 'c' union select 'd'
)
select text
from table
join value_list
on text ilike '%/www.abc.com/' || val || '/%'

2)使用OR:

select text,
   case 
        when text ilike '%/www.abc.com/a/%' 
        or text ilike '%/www.abc.com/b/%' 
        or text ilike '%/www.abc.com/c/%' 
        or text ilike '%/www.abc.com/d/%' 
        then 'URLType1'
        when <some other condition> 
        then 'URLType2'
   end as URLType

来自表格

3)编写一个Python UDF,它接受url和list并返回true或false,如下所示:

CREATE OR REPLACE FUNCTION multi_ilike(str varchar(max),arr varchar(max))
RETURNS boolean
STABLE AS $$
    if str==None or arr==None:
        return None
    arr = arr.split(',')
    str = str.lower()
    for i in arr:
        if i.lower() in str:
            return True
    return False
$$ LANGUAGE plpythonu;

select multi_ilike('<some url>/www.abc.com/a/<more url>','/www.abc.com/a/,/www.abc.com/b/,/www.abc.com/c/,/www.abc.com/d/'); -- returns true
select multi_ilike('<some url>/www.abc.com/F/<more url>','/www.abc.com/a/,/www.abc.com/b/,/www.abc.com/c/,/www.abc.com/d/'); -- returns false