I have a data sample of http://sqlfiddle.com/#!4/ecba5/4:
with raw_data as(select
'a{ thing_a<1234:1.1>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<F>>thing_y<F>>thing_z<F>>#thing_a<234:1.2>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<T>>thing_y<F>>thing_z<F>>#thing_a<345:1.3>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<T>>thing_y<F>>thing_z<F>>#thing_a<456:1.4>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<T>>thing_y<F>>thing_z<F>>#};ADD{some_thing<1234>>some_id<null>>some_stuff<2>>some_date<2013-07-09+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#some_thing<234>>some_id<null>>some_stuff<2>>some_date<2013-10-21+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#some_thing<345>>some_id<null>>some_stuff<2>>some_date<2013-07-22+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#some_thing<456>>some_id<null>>some_stuff<1>>some_date<2014-03-31+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#}]' value
from dual)
select
regexp_count(value, 'thing_a<\d*:\d\.\d*>') thing_a, -- count all occurences of thing_a
regexp_replace(value, 'thing_b<(-)>', '0'), -- How to only replace capturing group?
--regexp_substr(regexp_replace(value, 'thing_b<(-)>', '0'),'thing_b<(.)>', 1, 1, NULL, 1), -- assuming this works - how to sum it up? there is no regexp_sum to aggregate the capturing groups?
value
from raw_data;
Some aggregations are easy i.e. for thing_a
a regexp_count
works fine. However for all the others like thing_b
first a replacement of -
to NaN
and F
to 0
and T
to 1
should take place for each capturing group.
Then I want to sum it all up. But no regexp_sum
seems to exist.
Desired output would be similar to:
thing_a,thing_b,thing_d,...,thing_x
4,0,0,...,3
You could do this by replacing the whole match with a re-formatted version that removes the captured group:
regexp_replace(value, '(thing_b)<(-)>', '\1<0>')
And summing those values can be done with Oracle's built-in SUM():
sum(to_number(regexp_substr(regexp_replace(value, '(thing_b)<(-)>', '\1<0>'),'thing_b<(.)>', 1, 1, NULL, 1)))
The full query:
with raw_data as(select
'a{ thing_a<1234:1.1>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<F>>thing_y<F>>thing_z<F>>#thing_a<234:1.2>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<T>>thing_y<F>>thing_z<F>>#thing_a<345:1.3>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<T>>thing_y<F>>thing_z<F>>#thing_a<456:1.4>>thing_b<->>thing_c<T>>thing_d<F>>thing_f<F>>thing_g<F>>thing_h<F>>thing_i<F>>thing_x<T>>thing_y<F>>thing_z<F>>#};ADD{some_thing<1234>>some_id<null>>some_stuff<2>>some_date<2013-07-09+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#some_thing<234>>some_id<null>>some_stuff<2>>some_date<2013-10-21+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#some_thing<345>>some_id<null>>some_stuff<2>>some_date<2013-07-22+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#some_thing<456>>some_id<null>>some_stuff<1>>some_date<2014-03-31+02:00>>thing_zz<1>>foo_bar<0>>status_foo<0>>bar_value<0>>#}]' value
from dual)
select
regexp_count(value, 'thing_a<\d*:\d\.\d*>') thing_a, -- count all occurences of thing_a
regexp_replace(value, '(thing_b)<(-)>', '\1<0>'), -- How to only replace capturing group?
sum(to_number(regexp_substr(regexp_replace(value, '(thing_b)<(-)>', '\1<0>'),'thing_b<(.)>', 1, 1, NULL, 1))) -- assuming this works - how to sum it up? there is no regexp_sum to aggregate the capturing groups?
from raw_data;