2017-08-06 114 views
0

我有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; 

一些聚合很容易即對於thing_a一個regexp_count工作正常。然而對於所有其他人如thing_b首先應該爲每個捕獲組進行-NaNF0T1的替換。 然後我想總結一下。但似乎沒有regexp_sum存在。

所需的輸出將類似於:

regexp_replace(value, '(thing_b)<(-)>', '\1<0>') 

,總結這些值:

thing_a,thing_b,thing_d,...,thing_x 
4,0,0,...,3 

回答

1

你可以通過重新格式化的版本,刪除所拍攝的組更換整個比賽做到這一點可以用Oracle內置的SUM()完成:

sum(to_number(regexp_substr(regexp_replace(value, '(thing_b)<(-)>', '\1<0>'),'thing_b<(.)>', 1, 1, NULL, 1))) 

完整查詢:

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;