sql - Oracle REGEXP_LIKE match up to a period (decimal point) or find next sequence in numbers such as Dewey Decimal -
i have data such
oc_id parent_oc_id 4.0.1 4.0 4.0.1.1 4.0.1 4.0.1.1.1 4.0.1.1 4.0.1.1.2 4.0.1.1 4.0.1.1.3 4.0.1.1 4.0.1.2 4.0.1 4.0.1.3 4.0.1 4.0.1.4 4.0.1 4.0.1.5 4.0.1 4.0.1.5.1 4.0.1.5 4.0.1.5.2 4.0.1.5 4.0.1.5.3 4.0.1.5 4.0.1.5.4 4.0.1.5 4.0.2 4.0 4.0.2.1 4.0.2 4.0.2.2 4.0.2 4.0.2.2.1 4.0.2.2 4.0.12 4.2 <--------data anomally!!!
i trying determine next number in hierarchy. example if start parent_oc_id = 4.0
need know next available sibling number (oc_id) within 4.0.?
hierarchy 4.0.13
originally using , worked.
select max(to_number (substr (eb_ocs.oc_id, instr (eb_ocs.oc_id, '.', -1)+1)))+1 eb_ocs eb_ocs.parent_oc_id = '4.0'
but somehow parent siblings got out of sync , where
clause missing 4.0.12 sibling , max number worng (this risk of managing keys manually)
so based on data anomaly above 4.0
should return 4.0.13
4.0.3
i'm taking steps final query , trying search using regexp starting string , including doesn't have period (.)
something
select eb_ocs.oc_id eb_ocs regexp_like (eb_ocs.oc_id, '4.0.(.*)^.')
but negative isn't working.
ultimately want next sequence under particular level
4.0 should give 4.0.13 4 should give 4.1 4.0.1.1 should give 4.0.1.1.4
if column parent_ocs_id unreliable, simple ignore , calculate correct child key. rest original approach
fix_parent (select oc_id, substr (eb_ocs.oc_id, 1, instr (eb_ocs.oc_id, '.', -1)-1) parent_oc_id, to_number (substr (eb_ocs.oc_id, instr (eb_ocs.oc_id, '.', -1)+1)) child_number tst eb_ocs) select parent_oc_id, max(child_number) +1 next_child_number fix_parent parent_oc_id in ('4.0.1.1','4.0','4') group parent_oc_id order parent_oc_id;
.
parent_oc_id next_child_number ------------ ----------------- 4.0 13 4.0.1.1 4
to result parent '4' add line
insert tst values ('4.0','4');
Comments
Post a Comment