Get the sum of partial value in a column

时间:2017-06-14 10:29:33

标签: sql oracle

Consider there is a table tableA

col1 col2

1    some random string and number 1213 aa5 string aaasome number
2    some random string 432682 aa3 test
1    aa7

I need to get the result as below.

1   12
2    3

group by col1 and the result will be 5+7 (the partial int after the 'aa' string)

To add more clarity to the question,the col2 has some other strings as well.. like test test test aa2 again test test 23u45 ajsdk 4834... . Here i need to pick the 2 alone.

kindly suggest a solution for this.

2 个答案:

答案 0 :(得分:0)

你需要摆脱前缀,强制转换为数字和总和。一种方法如下:

<canvas id='myCanvas'></canvas>

答案 1 :(得分:0)

您可以使用正则表达式从字符串中获取所需的数字:

Select col1, sum(regexp_replace(col2,'(^|.*\s)aa(\d+)(\s.*|$)', '\2'))
From t
Group by col1

demo