Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. with phones_cte as (
  4.     select
  5.         *
  6.     from
  7.         "Contact" p
  8.     where
  9.         p."Contractor" = 21799108::integer and
  10.         p."Type" = any(array[10, 11, 12, 13, 14]) and
  11.         p."PrivatePerson" is NOT NULL and
  12.         p."Active"
  13.     limit null
  14.     ),
  15.     pack as (
  16.             select  *
  17.             from (
  18.                 select
  19.                     p
  20.                     ,unnest(array(
  21.                         select t
  22.                         from "Телефон" t
  23.                         where t."НомерПолный" = "substring"(p."Value", '([^|]{0,})'::text)
  24.                     )) a
  25.                 from
  26.                     phones_cte p
  27.             ) a
  28.         )
  29.  ,
  30.     matched as (
  31.         select
  32.             p,
  33.             a,
  34.             exists (
  35.                 with
  36.                 search_pack as (
  37.                         -- CTE для получения списка найденных телефонов.
  38.                         select '{'|| (p)."Value" || '}' phone_other
  39.                     ),
  40.  
  41.                 from_other as (
  42.                         with
  43.                         phones_other as (
  44.                             select unnest(p.phone_other::text[]) phone
  45.                             from search_pack p
  46.                         ),
  47.                         region as (
  48.                             select left(coalesce("KLADR", "КПП", "ИНН",'0'), 2) as region
  49.                             from "Контрагент"
  50.                             where "@Лицо" = 21799108::integer
  51.                         )
  52.                         select null
  53.                         from (
  54.                             select unnest(array(
  55.                                 SELECT
  56.                                     cd
  57.                                 FROM
  58.                                     "Contact" cd
  59.                                 WHERE
  60.                                     reverse(substring(cd."Value", '([^|]{0,})')) = reverse(p.phone)
  61.                                     AND cd."Type" = ANY(ARRAY[10, 11, 12, 13, 14])  -- все типы для телефонов
  62.                                     AND cd."Active"
  63.                                     AND (coalesce(cd."Contractor", 0) <> 21799108::integer)
  64.                                     AND
  65.                                         case
  66.                                         when  cd."Contractor" is not null then
  67.                                             exists (
  68.                                                 select null
  69.                                                 from "Контрагент" k
  70.                                                 where k."@Лицо" = cd."Contractor"
  71.                                                     and k."НеОтображается" is null
  72.                                                     and ((left(coalesce(k."KLADR", k."КПП", k."ИНН", '0'), 2) = (select region from region) and length(p.phone) between 5 and 7) or length(p.phone) > 7)
  73.                                             )
  74.                                         else
  75.                                             exists (
  76.                                                 select null
  77.                                                 from "Контрагент" e
  78.                                                 where e."Entrepreneur" = cd."PrivatePerson"
  79.                                                     and e."НеОтображается" is null
  80.                                                     and ((left(coalesce(e."KLADR", e."КПП", e."ИНН", '0'), 2) = (select region from region) and length(p.phone) between 5 and 7) or length(p.phone) > 7)
  81.                                             )
  82.                                         end
  83.                                 LIMIT 1
  84.                             )) t
  85.                             from phones_other p
  86.                             limit 1
  87.                         ) t
  88.                 )
  89.                  select null
  90.                 from
  91.                 from_other
  92.             ) "ЕстьСовпавшие"
  93.         from pack
  94.         limit 50
  95.     ),
  96.     counted as (
  97.         select
  98.             (p)."PrivatePerson",
  99.             array_agg(coalesce( (p)."Value"::text, (a)."НомерПолный"::text )) "НомерПолный",
  100.             array_agg(coalesce('8' || (a)."Код" || (a)."Номер"::text, (a)."НомерПолный"::text)) "НомерНорм",
  101.             array_agg((a)."Балл") "Баллы",
  102.             array_agg((a)."Статус") "Статусы",
  103.             array_agg((a)."@Телефон") "ИдТелефонов",
  104.            /* array_agg((p)."Value") "НомерПолный",
  105.             array_agg((p)."Value") "НомерНорм", */
  106.             array_agg("ЕстьСовпавшие") "ЕстьСовпавшие"
  107.         from
  108.             matched
  109.         group by
  110.             (p)."PrivatePerson"
  111.         )
  112.  
  113.         select
  114.             p."@Представитель" "Представитель",
  115.             (coalesce(pf."Фамилия",'') || coalesce(' ' || pf."Имя",'') || coalesce(' ' || pf."Отчество",'')) as "ФИО",
  116.             p."Примечание",
  117.            --  << phone section
  118.             phone."ИдТелефонов",
  119.             phone."НомерПолный",
  120.             phone."НомерНорм",
  121.             phone."Баллы",
  122.             phone."Статусы",
  123.             phone."ЕстьСовпавшие",
  124.             -- <<
  125.             ct."Value" as email,
  126.             pf."@Лицо" as "Лицо",
  127.             p."Тип"::smallint,
  128.             p."Должность",
  129.             p."Контрагент",
  130.             -- Отдельно ФИО для Сервиса Профилей при отображении представителей в карточке контрагента
  131.             pf."Фамилия",
  132.             pf."Имя",
  133.             pf."Отчество"
  134.         from
  135.             "Представитель" p
  136.             inner join "ЧастноеЛицо" pf on pf."@Лицо" = p."ЧастноеЛицо" and pf."Тип" in (1, 2)
  137.             left join "Contact" ct on ct."PrivatePerson" = pf."@Лицо" and ct."Type"= 20
  138.             left join counted phone on p."ЧастноеЛицо" = phone."PrivatePerson"
  139.         where
  140.             p."Контрагент" = 21799108::integer
  141.         order by
  142.             "ФИО" nulls last, "НомерПолный" nulls last
  143.  
  144. -- select "Value", count(*)
  145. -- from "Contact"
  146. -- where "Contractor" =  21799108
  147. -- group by "Value"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement