我有这个redshift SQL查询。我使用“REGEXP_SUBSTR”函数从注释中提取了带小数的数字。我还需要将它从字符串转换为数字/小数。然后,我需要从总数中减去该数字。
这是我的查询
SELECT sc.comment,
sm.subtotal,
to_number(REGEXP_SUBSTR(sc.comment, '[0.00-9]+..[0.00-9]+', 1),'9999999D99')
FROM "sales_memo_comments" sc INNER JOIN "sales_memo" sm ON sc.foreign_id = sm.parent_id
我尝试在Redshift SQL上使用“to_number”函数,但它给出了以下内容:错误:类型为numeric的输入语法无效:“”
这是当前输出之前从评论栏中提取退款金额:
comment
"SAR719.00 Refund transaction executed successfully, Refund Request ID:504081288877953603 \n , Authorization Code:095542 "
"AUD52.07 Refund transaction executed successfully, Refund Request ID:6J45695858A90833"
Canceled by : ron.johnd@company.co.us
refund amount is [MYR197.41]
"Please Ignore Order refunded by Refund Request ID:5002758809696048 , Authorization Code:2587759"
OMR37.83($98.23) Refund transaction executed successfully
这是在将上述SQL查询与REGEXP一起使用之后。我仍然有些异常。
comment
719
52.07
.co.
197.41
5.0027621
37.83($98.23
两个问题
任何帮助都将不胜感激。
答案 0 :(得分:0)
这是一种方法 - 您需要能够测试字符串是否为数字,并且您需要UDF - 所以只需运行一次以定义该函数
create or replace function isnumeric (aval VARCHAR(20000))
returns bool
IMMUTABLE
as $$
try:
x = int(aval);
except:
return (1==2);
else:
return (1==1);
$$ language plpythonu;
然后,您可以按如下方式更改代码
SELECT sc.comment,
sm.subtotal,
to_number(
case when isnumeric(REGEXP_SUBSTR(sc.comment, '[0.00-9]+..[0.00-9]+', 1))
then REGEXP_SUBSTR(sc.comment, '[0.00-9]+..[0.00-9]+', 1)
else 0 end
,'9999999D99')
FROM "sales_memo_comments" sc INNER JOIN "sales_memo" sm ON sc.foriegn_id = sm.parent_id
答案 1 :(得分:0)
我的方法是首先添加两列:一列用于字符串长度,另一列用于计算允许的字符。从此表中,您可以仅筛选两个匹配的行(即没有不允许的字符),然后将剩余的值转换为浮点数或小数或其他任何值。
with temp as (
SELECT '719' as comment
UNION SELECT '52.07'
UNION SELECT '.co.'
UNION SELECT '197.41'
UNION SELECT '5.0027621'
UNION SELECT '37.83($98.23'
),
temp2 as (
SELECT *
,regexp_count(comment, '[0-9.]') as good_char_length
,len(comment) as str_length
FROM
temp
)
SELECT *
,comment::float as comment_as_float
FROM
temp2
WHERE
good_char_length = str_length