Amazon redshift 在红移中的特殊符号后计数数字?

Amazon redshift 在红移中的特殊符号后计数数字?,amazon-redshift,Amazon Redshift,我有一个字段值,其格式如下: GPU#nDisplays:Intel(R) Iris(TM) Graphics 540#1 Microsoft Basic Render Driver#0 在这里,我想要一个在“#”之后的值的总和。例如,在这种情况下,我的和将是1+0=1 如何使用Redshift SQL查询获得该结果 我一开始就尝试过这样的方法: (regexp_count(REGEXP_SUBSTR (cpu_params,'GPU#n[^,]*'),'#[0-9]') 您可以为此使用红移

我有一个字段值,其格式如下:

GPU#nDisplays:Intel(R) Iris(TM) Graphics 540#1 Microsoft Basic Render Driver#0
在这里,我想要一个在“#”之后的值的总和。例如,在这种情况下,我的和将是1+0=1

如何使用Redshift SQL查询获得该结果

我一开始就尝试过这样的方法:

(regexp_count(REGEXP_SUBSTR (cpu_params,'GPU#n[^,]*'),'#[0-9]')

您可以为此使用红移python UDF

重要信息-您需要:

revoke usage on language PLPYTHONU from PUBLIC;
grant usage on language PLPYTHONU to group udf_devs;
or
grant usage on language PLPYTHONU to user youruserid;
请原谅我的非pythonicpython代码-我相信这是可以改进的

只需将“yourstring”替换为测试字符串,并以红移方式运行以下命令

create or replace function f_sum_after_hash (s varchar(max))
  returns integer
stable
as $$
def f_sum_after_hash(input_string):
    parts = input_string.split("#")
    score = 0
    for part in parts[1:]:
        index=0
        for c in part:
            if c.isdigit():
                index+=1
            else:
                break
        if index >0:
            score+=int(part[:index])
    return score
return f_sum_after_hash(s)
$$ language plpythonu;

select f_sum_after_hash ('yourstring');

您可以为此使用红移python UDF

重要信息-您需要:

revoke usage on language PLPYTHONU from PUBLIC;
grant usage on language PLPYTHONU to group udf_devs;
or
grant usage on language PLPYTHONU to user youruserid;
请原谅我的非pythonicpython代码-我相信这是可以改进的

只需将“yourstring”替换为测试字符串,并以红移方式运行以下命令

create or replace function f_sum_after_hash (s varchar(max))
  returns integer
stable
as $$
def f_sum_after_hash(input_string):
    parts = input_string.split("#")
    score = 0
    for part in parts[1:]:
        index=0
        for c in part:
            if c.isdigit():
                index+=1
            else:
                break
        if index >0:
            score+=int(part[:index])
    return score
return f_sum_after_hash(s)
$$ language plpythonu;

select f_sum_after_hash ('yourstring');

嘿@akshay_rao为你做了下面的回答?如果是,请接受正确答案-如果不是,请解释原因?嘿@akshay_rao我下面的答案对你有用吗?如果是,请接受正确答案-如果不是,请解释原因?谢谢您的回复@Jon。但是,对于我使用的AWS红移,我们没有编写UDF的选项。我正在尝试一个类似的选项,在转储文件上使用python脚本。所有版本的redshift都有udf功能。请参阅。我已经在答案中添加了这一步。谢谢你的回复@Jon。但是,对于我使用的AWS红移,我们没有编写UDF的选项。我正在尝试一个类似的选项,在转储文件上使用python脚本。所有版本的redshift都有udf功能。请参阅。我已经在答案中添加了这一步骤。