Загрузка данных


with o as (
    select 'ООО|ОАО|ЗАО|ПАО|НАО|АО|АООТ|АОЗТ|ТОО|КТ|ФГУП|ФГУ|ГУП|ГКУП|ГП|МУП|НОУ|АНО|НКО|АКБ|КБ|АБ|АК|СПК|НПФ|НПО|ИНК|ЛТД|ЛИМИТЕД|ГМБХ|ПЛС' as opf,
           'ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ|([А-Я]+ )?АКЦИОНЕРНОЕ ОБЩЕСТВО|ФОНД|КОМПАНИЯ|КОМПАНИИ|ФИРМА|КОРПОРАЦИЯ' as dlin,
           '( *\([^)]*\))?((( +[А-ЯA-Z0-9-]+){1,2}( +"[^"]+|"[А-ЯA-Z0-9][^"]*)| *"[^"]+)("[А-ЯA-Z0-9][^"]*)?("|$)| +\.[^.]+\.|( +[А-ЯA-Z0-9-]+)+)' as hvost,
           ' +(([А-Я-]{3,}(СКОЕ|СКОМ|СКИЙ|СКОЙ|СКОГО|НОЕ|НОМ|НЫЙ|НОЙ|НОГО|ЕГО) +)?((ОСБ|ФИЛИАЛ|Ф-Л|ФИЛ|ОТДЕЛЕНИЕ|ОТД|СЧЕТ|СЧ|ДОП|Р|Л|Г|В|N|ТЕЛ|ФО|СБ +РФ)([^А-Я].*)?|(КПП|ИНН|БИК)( *[^А-Я ].*)?)|[0-9]{5,}.*)$' as stop,
           'ОБЩЕСТВ|ТОВАРИЩЕСТВ|КООПЕРАТИВ|РАЙПО|АССОЦ|СОЮЗ|ОРГАНИЗАЦ|ОРГ\.|ОБЪЕДИНЕНИ|ПАРТНЕРСТВ|АКЦИОНЕРН' as kl,
           '(^|[^А-Я])(ИП|ПБОЮЛ|БОЮЛ|ЧП)([^А-Я]|$)|ПРЕДПРИН' as ip
    from dual
),
t as (
    select distinct naimen,
           translate(upper(naimen),
                     'ЁABCEHKMOPTXY«»“”„''<>^?' || unistr('\00A0') || chr(9) || chr(10) || chr(13),
                     'ЕАВСЕНКМОРТХУ' || '""""""""""' || '    ') as u
    from sh.uch_vse
    where naimen is not null
),
m as (
    select t.naimen, t.u, o.stop, o.ip, o.opf,
           regexp_replace(coalesce(
               regexp_substr(t.u, '(^|[^А-ЯA-Z0-9])(' || o.opf  || ')' || o.hvost),
               regexp_substr(t.u, '(^|[^А-ЯA-Z0-9])(' || o.dlin || ')' || o.hvost)),
             '^[^А-ЯA-Z0-9]', '') as m1,
           regexp_replace(
               regexp_substr(t.u, '^[^А-ЯA-Z0-9]*(ООО|ОАО|ПАО|АО)[А-ЯA-Z0-9-]+( +[А-ЯA-Z0-9-]+)*'),
             '^[^А-ЯA-Z0-9]+', '') as mg,
           regexp_substr(t.u,
               '^[^А-ЯA-Z0-9]*([А-ЯA-Z0-9][А-ЯA-Z0-9 ,.()-]*?[ ,(]+(' || o.opf || ')\)?)([^А-ЯA-Z0-9]|$)',
               1, 1, null, 1) as m2,
           regexp_substr(t.u, '"[^"]+("[А-ЯA-Z0-9][^"]*)?("|$)') as m3,
           case when regexp_like(t.u, o.kl) then regexp_substr(t.u, '[А-ЯA-Z0-9"].*') end as m4
    from t cross join o
),
c as (
    select naimen, u, ip, opf, m2,
           case when m1 like '%"' then m1 else regexp_replace(m1, stop, '') end as m1,
           regexp_replace(mg, stop, '') as mg,
           case when m3 like '%"' then m3 else regexp_replace(m3, stop, '') end as m3,
           regexp_replace(m4, stop, '') as m4
    from m
),
r as (
    select naimen, u,
           case when mg is not null then mg
                when instr(m1, '"') > 0 or instr(m1, ' ') > 0 then m1
                when m2 is not null
                     and not regexp_like(m2, '(^| )(В|ИП|ПБОЮЛ)( |$)') and not regexp_like(m2, '^(' || opf || ')[ ,(]') then m2
                when length(m4) >= 3
                     and not regexp_like(u, ip) then m4
                when regexp_like(m3, '[А-ЯA-Z0-9]{2}')
                     and not regexp_like(m3, '^" *[А-Я-]+ +[А-Я]\. ?[А-Я]\.')
                     and not regexp_like(u, ip) then m3
           end as org_u
    from c
),
res as (
    select naimen,
           regexp_replace(rtrim(substr(naimen, instr(u, org_u), length(org_u)), ' ,.;-'), ' {2,}', ' ') as org
    from r
)
select distinct org as "Наименование юрлица"
from res
where org is not null
order by 1;



with o as (
    select 'ООО|ОАО|ЗАО|ПАО|НАО|АО|АООТ|АОЗТ|ТОО|КТ|ФГУП|ФГУ|ГУП|ГКУП|ГП|МУП|НОУ|АНО|НКО|АКБ|КБ|АБ|АК|СПК|НПФ|НПО|ИНК|ЛТД|ЛИМИТЕД|ГМБХ|ПЛС' as opf,
           'ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ|([А-Я]+ )?АКЦИОНЕРНОЕ ОБЩЕСТВО|ФОНД|КОМПАНИЯ|КОМПАНИИ|ФИРМА|КОРПОРАЦИЯ' as dlin,
           '( *\([^)]*\))?((( +[А-ЯA-Z0-9-]+){1,2}( +"[^"]+|"[А-ЯA-Z0-9][^"]*)| *"[^"]+)("[А-ЯA-Z0-9][^"]*)?("|$)| +\.[^.]+\.|( +[А-ЯA-Z0-9-]+)+)' as hvost,
           ' +(([А-Я-]{3,}(СКОЕ|СКОМ|СКИЙ|СКОЙ|СКОГО|НОЕ|НОМ|НЫЙ|НОЙ|НОГО|ЕГО) +)?((ОСБ|ФИЛИАЛ|Ф-Л|ФИЛ|ОТДЕЛЕНИЕ|ОТД|СЧЕТ|СЧ|ДОП|Р|Л|Г|В|N|ТЕЛ|ФО|СБ +РФ)([^А-Я].*)?|(КПП|ИНН|БИК)( *[^А-Я ].*)?)|[0-9]{5,}.*)$' as stop,
           'ОБЩЕСТВ|ТОВАРИЩЕСТВ|КООПЕРАТИВ|РАЙПО|АССОЦ|СОЮЗ|ОРГАНИЗАЦ|ОРГ\.|ОБЪЕДИНЕНИ|ПАРТНЕРСТВ|АКЦИОНЕРН' as kl,
           '(^|[^А-Я])(ИП|ПБОЮЛ|БОЮЛ|ЧП)([^А-Я]|$)|ПРЕДПРИН' as ip
    from dual
),
t as (
    select distinct naimen,
           translate(upper(naimen),
                     'ЁABCEHKMOPTXY«»“”„''<>^?' || unistr('\00A0') || chr(9) || chr(10) || chr(13),
                     'ЕАВСЕНКМОРТХУ' || '""""""""""' || '    ') as u
    from sh.uch_vse
    where naimen is not null
),
m as (
    select t.naimen, t.u, o.stop, o.ip, o.opf,
           regexp_replace(coalesce(
               regexp_substr(t.u, '(^|[^А-ЯA-Z0-9])(' || o.opf  || ')' || o.hvost),
               regexp_substr(t.u, '(^|[^А-ЯA-Z0-9])(' || o.dlin || ')' || o.hvost)),
             '^[^А-ЯA-Z0-9]', '') as m1,
           regexp_replace(
               regexp_substr(t.u, '^[^А-ЯA-Z0-9]*(ООО|ОАО|ПАО|АО)[А-ЯA-Z0-9-]+( +[А-ЯA-Z0-9-]+)*'),
             '^[^А-ЯA-Z0-9]+', '') as mg,
           regexp_substr(t.u,
               '^[^А-ЯA-Z0-9]*([А-ЯA-Z0-9][А-ЯA-Z0-9 ,.()-]*?[ ,(]+(' || o.opf || ')\)?)([^А-ЯA-Z0-9]|$)',
               1, 1, null, 1) as m2,
           regexp_substr(t.u, '"[^"]+("[А-ЯA-Z0-9][^"]*)?("|$)') as m3,
           case when regexp_like(t.u, o.kl) then regexp_substr(t.u, '[А-ЯA-Z0-9"].*') end as m4
    from t cross join o
),
c as (
    select naimen, u, ip, opf, m2,
           case when m1 like '%"' then m1 else regexp_replace(m1, stop, '') end as m1,
           regexp_replace(mg, stop, '') as mg,
           case when m3 like '%"' then m3 else regexp_replace(m3, stop, '') end as m3,
           regexp_replace(m4, stop, '') as m4
    from m
),
r as (
    select naimen, u,
           case when mg is not null then mg
                when instr(m1, '"') > 0 or instr(m1, ' ') > 0 then m1
                when m2 is not null
                     and not regexp_like(m2, '(^| )(В|ИП|ПБОЮЛ)( |$)') and not regexp_like(m2, '^(' || opf || ')[ ,(]') then m2
                when length(m4) >= 3
                     and not regexp_like(u, ip) then m4
                when regexp_like(m3, '[А-ЯA-Z0-9]{2}')
                     and not regexp_like(m3, '^" *[А-Я-]+ +[А-Я]\. ?[А-Я]\.')
                     and not regexp_like(u, ip) then m3
           end as org_u
    from c
),
res as (
    select naimen,
           regexp_replace(rtrim(substr(naimen, instr(u, org_u), length(org_u)), ' ,.;-'), ' {2,}', ' ') as org
    from r
)
select naimen as "Наименование"
from res
where org is null
order by 1;