with params as (select 
cast ($1 as bigint) as p_lpu_id ,
cast ($2 as bigint) as p_orgsmo_id ,
cast(to_timestamp($3, 'DD.MM.YYYY') as timestamp) as p_begdate,
cast(to_timestamp($4, 'DD.MM.YYYY') as timestamp) as p_enddate
),
Lpu_W as(
select 
L.lpu_id,
L.lpu_Nick,
params.*
from v_Lpu L
inner join params on 1=1
where (L.Lpu_id = p_lpu_id or p_lpu_id is null and COALESCE(L.Lpu_IsTest, 1) = 1)
),
Registry_W as (
select
'oblast,inog' as katnasel_sysnick,
r.Registry_id,
r.Registry_Num,
r.RegistryType_id,
r.RegistryStatus_id,
cast(r.Registry_accDate as date) as Registry_accDate,
make_date(date_part('year',p_enddate)::int4,1,1) as ds,
lp.*
from Lpu_W lp
inner join r89.Registry r  on r.Lpu_id=lp.Lpu_id
and cast(r.Registry_accDate as date) between make_date(date_part('year',p_enddate)::int4,1,1) and p_enddate
and r.RegistryStatus_id in (2,3,4)
and coalesce(r.Registry_deleted,1)=1
and r.RegistryType_id in (1,2,6,20,7,9,11,12) --1 - стациоанар, 2 - поликлиника,  6 - СМП, 7 - ДВН, 9 - ДДС, 11 - ПОВН, 12 - МОН , 20 - взаиморасчеты
--Реестры ОМС Коми
),
--поликлиника
Registry_polka as (select
r.katnasel_sysnick,
'polka' as rtype,
case when msf.MedicalCareKind_id in (1,5,6,7) then 'pm' when msf.MedicalCareKind_id in (2,4,8) then 'sm' end as CareKind,
r.Registry_Num,
case when OMSSprTerr.KLRgn_id=dbo.GetRegion() then 1 else 2 end as terrstrach,
r.RegistryType_id,
r.RegistryStatus_id,
ev.EvnVizitPl_id as Evn_id,
case when rd.evn_id=ev.EvnVizitPl_id then rd.RegistryData_ItogSum else 0 end as RegistryData_ItogSum,
rd.Person_id,
RD.Paid_id,
r.Registry_accDate,
ev.EvnClass_id,
ev.MedStaffFact_id,
OMSSprTerr.KLRgn_id,
r.p_lpu_id ,
r.p_orgsmo_id ,
r.p_begdate,
r.p_enddate,
r.ds,
null::numeric as RegistryData_KdFact,
null::bigint as LpuUnitType_id
from Registry_w r
inner join r89.registryData RD   on R.Registry_id=RD.Registry_id
and (RD.OrgSmo_id=p_orgsmo_id or p_orgsmo_id is null)
inner join v_EvnVizitPl ev  on RD.Evn_rid=ev.EvnVizitPl_pid
inner join v_paytype pt on pt.paytype_id=ev.paytype_id
and pt.paytype_sysnick='oms'
--Действующий полис на конец отчетного периода 
inner join lateral (
select Pol.Polis_id,
Pol.OmsSprTerr_id
from v_PersonPolis Pol
where Pol.Person_id = rd.Person_id 
and (cast (Pol.Polis_begDate as date) <= p_enddate 
and ((cast (Pol.Polis_endDate as date))>=p_enddate or Pol.Polis_endDate  is null))
order by Pol.Polis_begDate desc limit 1
) Pol on true
inner join OMSSprTerr OMSSprTerr  on OMSSprTerr.OMSSprTerr_id=Pol.OMSSprTerr_id
inner join v_MedStaffFact msf  on msf.MedStaffFact_id=ev.MedStaffFact_id
and msf.MedicalCareKind_id in (1,2,4,5,6,7,8)
where  r.RegistryType_id=2
and coalesce(rd.RegistryData_deleted,1)=1
and not exists (
select *
from  r89.RegistryError RE 
inner join r89.RegistryErrorType RET on ret.registryerrortype_id = RE.registryerrortype_id
and RET.registryerrorclass_id=1 -- Ошибка
where re.Registry_id=r.Registry_id and RE.evn_id=RD.evn_id
)
and not exists (
select *
from r89.RegistryErrorTFOMS RET1
where RET1.Evn_id = RD.Evn_id
and RET1.Registry_id = R.Registry_id
and coalesce(RET1.RegistryErrorTfomsLevel_id, 1) = 1
)
),
--Взаиморасчеты для Вологды
--Диспансеризация
Registry_dispVizit as (select 
r.katnasel_sysnick,
'disp' as rtype,
case when msf.MedicalCareKind_id in (1,5,6,7) then 'pm' when msf.MedicalCareKind_id in (2,4,8) then 'sm' end as CareKind,
r.Registry_Num,
case when OMSSprTerr.KLRgn_id=dbo.GetRegion() then 1 else 2 end as terrstrach,
r.RegistryType_id,
r.RegistryStatus_id,
ev.EvnVizitDisp_id as Evn_id,
case when rd.Evn_id=ev.EvnVizitDisp_id then Rd.RegistryData_ItogSum else 0 end as RegistryData_ItogSum,
rd.Person_id,
RD.Paid_id,
r.Registry_accDate,
11 as EvnClass_id,
ev.MedStaffFact_id,
OMSSprTerr.KLRgn_id,
r.p_lpu_id ,
r.p_orgsmo_id ,
r.p_begdate,
r.p_enddate,
r.ds,
null::numeric as RegistryData_KdFact,
null::bigint as LpuUnitType_id
from Registry_w r
inner join r89.RegistryData rd on rd.Registry_id=r.Registry_id
inner join v_EvnPLDisp EvnPLDisp  on EvnPLDisp.EvnPLDisp_id=rd.Evn_rid
and EvnPLDisp.DispClass_id in (1,2,3,4,7,8,5,6,9,10,11,12) 
--1,2 - ДВН 1-ый и 2-ой этап
--6,9,10,11,12 - МОН 1-ый и 2-ой этап
--5 - ПОВН
--3,4,7,8 - ДДС 1-ый и 2-ой этап
inner join v_EvnVizitDisp ev  on EvnVizitDisp_pid=EvnPLDisp.EvnPLDisp_id
inner join v_paytype pt on pt.paytype_id=EvnPLDisp.paytype_id
and pt.paytype_sysnick='oms'
inner join v_MedStaffFact msf  on msf.MedStaffFact_id=ev.MedStaffFact_id
and msf.MedicalCareKind_id in (1,2,4,5,6,7,8)
--Действующий полис на конец отчетного периода 
inner join lateral (
select Pol.Polis_id,
Pol.OmsSprTerr_id
from v_PersonPolis Pol
where Pol.Person_id = rd.Person_id 
and (cast (Pol.Polis_begDate as date) <= p_enddate 
and ((cast (Pol.Polis_endDate as date))>=p_enddate or Pol.Polis_endDate  is null))
order by Pol.Polis_begDate desc limit 1
) Pol on true
inner join OMSSprTerr OMSSprTerr  on OMSSprTerr.OMSSprTerr_id=Pol.OMSSprTerr_id
inner join v_DopdispInfoConsent DDIC  on DDIC.DopdispInfoConsent_id = EV.DopDispInfoConsent_id
inner join v_SurveyTypeLink STL  on STL.SurveyTypeLink_id = DDIC.SurveyTypeLink_id
inner join v_SurveyType ST  on ST.SurveyType_id = STL.SurveyType_id
and st.surveytype_name like '%врач%' 
and coalesce(ST.SurveyType_IsVizit, 1)=2--только визит
where  coalesce(rd.RegistryData_deleted,1)=1
and r.RegistryType_id in (7,9,11,12)
and not exists (
select *
from  r89.RegistryError RE 
inner join r89.RegistryErrorType RET on ret.registryerrortype_id = RE.registryerrortype_id
and RET.registryerrorclass_id=1 -- Ошибка
where re.Registry_id=r.Registry_id and RE.evn_id=RD.evn_id
)
and not exists (
select *
from r89.RegistryErrorTFOMS RET1
where RET1.Evn_id = RD.Evn_id
and RET1.Registry_id = R.Registry_id
and coalesce(RET1.RegistryErrorTfomsLevel_id, 1) = 1
)
),
Registry_stac as (select
r.katnasel_sysnick,
'stac' as rtype,
case when msf.MedicalCareKind_id in (1,5,6,7) then 'pm' when msf.MedicalCareKind_id in (2,4,8) then 'sm' end as CareKind,
r.Registry_Num,
case when OMSSprTerr.KLRgn_id=dbo.GetRegion() then 1 else 2 end as terrstrach,
r.RegistryType_id,
r.RegistryStatus_id,
es.EvnSection_id as Evn_id,
case when rd.evn_id=es.EvnSection_id then rd.RegistryData_ItogSum else 0 end as RegistryData_ItogSum,
rd.Person_id,
RD.Paid_id,
r.Registry_accDate,
es.EvnClass_id,
es.MedStaffFact_id,
OMSSprTerr.KLRgn_id,
r.p_lpu_id ,
r.p_orgsmo_id ,
r.p_begdate,
r.p_enddate,
r.ds,
case when rd.evn_id=es.EvnSection_id then rd.RegistryData_KdFact else 0 end as RegistryData_KdFact,
RD.LpuUnitType_id
from Registry_w r
inner join r89.registryData RD  on R.Registry_id=RD.Registry_id
and (RD.OrgSmo_id=p_orgsmo_id or p_orgsmo_id is null)
and coalesce(rd.RegistryData_deleted,1)=1
inner join v_EvnSection ES  on RD.Evn_id=ES.EvnSection_id
inner join v_paytype pt on pt.paytype_id=es.paytype_id
and pt.paytype_sysnick='oms'
inner join v_MedStaffFact msf  on msf.MedStaffFact_id=es.MedStaffFact_id
and msf.MedicalCareKind_id in (1,2,4,5,6,7,8)
--Действующий полис на конец отчетного периода 
inner join lateral (
select Pol.Polis_id,
Pol.OmsSprTerr_id
from v_PersonPolis Pol
where Pol.Person_id = rd.Person_id 
and (cast (Pol.Polis_begDate as date) <= p_enddate 
and ((cast (Pol.Polis_endDate as date))>=p_enddate or Pol.Polis_endDate  is null))
order by Pol.Polis_begDate desc limit 1
) Pol on true
inner join OMSSprTerr OMSSprTerr  on OMSSprTerr.OMSSprTerr_id=Pol.OMSSprTerr_id
where  R.RegistryType_id =1
and not exists (
select *
from  r89.RegistryError RE
inner join r89.RegistryErrorType RET on ret.registryerrortype_id = RE.registryerrortype_id
and RET.registryerrorclass_id=1 -- Ошибка
where re.Registry_id=r.Registry_id and RE.evn_id=RD.evn_id
)
and not exists (
select *
from r89.RegistryErrorTFOMS RET1
where RET1.Evn_id = RD.Evn_id
and RET1.Registry_id = R.Registry_id
and coalesce(RET1.RegistryErrorTfomsLevel_id, 1) = 1
)
) ,
Registry_SMP as (
select 
r.katnasel_sysnick,
'smp' as rtype,
case when msf.MedicalCareKind_id in (1,5,6,7) then 'pm' when msf.MedicalCareKind_id in (2,4,8) then 'sm' end as CareKind,
r.Registry_Num,
case when OMSSprTerr.KLRgn_id=dbo.GetRegion() then 1 else 2 end as terrstrach,
r.RegistryType_id,
r.RegistryStatus_id,
ccc.CmpCallCard_id as Evn_id,
rd.RegistryDataCmp_ItogSum as RegistryData_ItogSum,
rd.Person_id,
RD.Paid_id,
r.Registry_accDate,
null::bigint as EvnClass_id,
null::bigint as MedStaffFact_id,
null::bigint as KLRgn_id,
r.p_lpu_id ,
r.p_orgsmo_id ,
r.p_begdate,
r.p_enddate,
r.ds,
null::numeric as RegistryData_KdFact,
null::bigint as LpuUnitType_id
from Registry_w r
inner join r89.RegistryDataCmp RD  on R.Registry_id=RD.Registry_id
and (RD.OrgSmo_id=p_orgsmo_id or p_orgsmo_id is null)
and coalesce(rd.RegistryDataCmp_deleted,1)=1
inner join v_CmpCloseCard as cclc on rd.CmpCloseCard_id = cclc.CmpCloseCard_id
inner join v_CmpCallCard as ccc on	ccc.CmpCallCard_id = cclc.CmpCallCard_id
inner join v_paytype pt on pt.paytype_id=ccc.paytype_id
and pt.paytype_sysnick='oms'
inner join v_MedStaffFact msf  on msf.MedStaffFact_id=ccc.MedStaffFact_id
and msf.MedicalCareKind_id in (1,2,4,5,6,7,8)
--Действующий полис на конец отчетного периода 
inner join lateral (
select Pol.Polis_id,
Pol.OmsSprTerr_id
from v_PersonPolis Pol
where Pol.Person_id = rd.Person_id 
and (cast (Pol.Polis_begDate as date) <= p_enddate 
and ((cast (Pol.Polis_endDate as date))>=p_enddate or Pol.Polis_endDate  is null))
order by Pol.Polis_begDate desc limit 1
) Pol on true
inner join OMSSprTerr OMSSprTerr  on OMSSprTerr.OMSSprTerr_id=Pol.OMSSprTerr_id
where  R.RegistryType_id = 6
/*errorCmp_rem*/
and not  exists (
select *
from  r89.RegistryErrorCmp RE 
inner join r89.RegistryErrorType RET on ret.registryerrortype_id = RE.registryerrortype_id
and RET.registryerrorclass_id=1 -- Ошибка
where re.Registry_id=r.Registry_id and RE.CmpCloseCard_id=RD.CmpCloseCard_id
)
/*/errorCmp_rem*/
/*errorCmp_add
and not  exists (
select *
from  r89.RegistryErrorCmp RE 
inner join r89.RegistryErrorType RET on ret.registryerrortype_id = RE.registryerrortype_id
and RET.registryerrorclass_id=1 -- Ошибка
where re.Registry_id=r.Registry_id and RE.CmpCallCard_id=RD.CmpCallCard_id
)
/errorCmp_add*/
),
registryAll as (
select * From Registry_polka
union
select*from Registry_dispVizit
union
select*From Registry_stac
union
select*From Registry_SMP
)
select 
--Раздел 1
--Первичная медико-санитарная помощь
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then rd.Evn_id end) as v1_4,  
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm')  then rd.Evn_id end) as v1_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm')  then rd.Person_id end) as v1_6, 
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm')  then rd.Person_id end) as v1_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v1_8, 
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between ds  and p_enddate and rd.CareKind in ('pm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v1_9, --Первичная медико-
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then rd.Evn_id end) as v2_4,  
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') then rd.Evn_id end) as v2_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then rd.Person_id end) as v2_6, 
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm')  then rd.Person_id end) as v2_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v2_8, 
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between ds  and p_enddate and rd.CareKind in ('pm')  then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v2_9, --Первичная медико-
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v3_4,
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v3_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v3_6, 
count(distinct case  when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v3_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v3_8,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v3_9,
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v4_4,
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v4_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v4_6, 
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v4_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v4_8,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v4_9,
--Специализированная медицинская помощь
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v5_4,  
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v5_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v5_6, 
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v5_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm')then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v5_8, 
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between ds  and p_enddate and rd.CareKind in ('sm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v5_9,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v6_4,  
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v6_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v6_6, 
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v6_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm')then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v6_8, 
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v6_9, --Первичная медико-
--помощь,оказанная в условиях дневных стационаров всех типов
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v7_4,
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v7_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v7_6,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v7_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v7_8,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v7_9,
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v8_4,
sum(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v8_5,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v8_6,
count(distinct case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v8_7,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v8_8,
round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v8_9,
--СМП
coalesce(count(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date) between p_begdate and p_enddate then rd.Evn_id end),0) as v9_4,
coalesce(count(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date)  between ds and p_enddate then rd.Evn_id end),0) as v9_5,
coalesce(count(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date)  between p_begdate and p_enddate then rd.Person_id end),0) as v9_6,
coalesce(count(case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date)  between ds and p_enddate then rd.Person_id end),0) as v9_7,
coalesce(round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and RD.Paid_id=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date) between p_begdate and p_enddate then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2),0) as v9_8, --Первичная медико-санитарная помощь Сумма оплаченых поликлинических посещений за месяц 
coalesce(round((sum (case when rd.katnasel_sysnick like '%oblast%' and rd.terrstrach=1 and RD.Paid_id=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date) between ds  and p_enddate  then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2),0) as v9_9, --Первичная медико-санитарная помощь Сумма оплаченых поликлинических посещений с начала года
--Раздел 2
--Первичная медико-санитарная помощь
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then rd.Evn_id end) as v1_4a,  
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm')  then rd.Evn_id end) as v1_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm')  then rd.Person_id end) as v1_6a, 
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm')  then rd.Person_id end) as v1_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v1_8a, 
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr','disp') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between ds  and p_enddate and rd.CareKind in ('pm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v1_9a, --Первичная медико-
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then rd.Evn_id end) as v2_4a,  
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') then rd.Evn_id end) as v2_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then rd.Person_id end) as v2_6a, 
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('pm')  then rd.Person_id end) as v2_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v2_8a, 
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr')  and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between ds  and p_enddate and rd.CareKind in ('pm')  then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v2_9a, --Первичная медико-
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v3_4a,
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v3_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v3_6a, 
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v3_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v3_8a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac')  and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v3_9a,
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v4_4a,
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v4_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v4_6a, 
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v4_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v4_8a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('pm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v4_9a,
--Специализированная медицинская помощь
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v5_4a,  
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v5_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v5_6a, 
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v5_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm')then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v5_8a, 
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=11 and RD.Paid_id=2 and Registry_accDate between ds  and p_enddate and rd.CareKind in ('sm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v5_9a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v6_4a,  
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Evn_id end) as v6_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v6_6a, 
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then rd.Person_id end) as v6_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm')then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v6_8a, 
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('polka','vzaimr') and rd.EvnClass_id=13 and RD.Paid_id=2 and Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v6_9a, --Первичная медико-
--помощь,оказанная в условиях дневных стационаров всех типов
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v7_4a,
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.RegistryData_KdFact end) as v7_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v7_6a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then rd.Person_id end) as v7_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v7_8a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=2 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v7_9a,
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v8_4a,
sum(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.RegistryData_KdFact end) as v8_5a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v8_6a,
count(distinct case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then rd.Person_id end) as v8_7a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between p_begdate and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v8_8a,
round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('stac') and RD.Paid_id=2 and rd.Registry_accDate between ds and p_enddate and rd.CareKind in ('sm') and RD.LpuUnitType_id=1 then coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2) as v8_9a,
--СМП
coalesce(count(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date) between p_begdate and p_enddate then rd.Evn_id end),0) as v9_4a,
coalesce(count(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date)  between ds and p_enddate then rd.Evn_id end),0) as v9_5a,
coalesce(count(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date)  between p_begdate and p_enddate then rd.Person_id end),0) as v9_6a,
coalesce(count(case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date)  between ds and p_enddate then rd.Person_id end),0) as v9_7a,
coalesce(round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and RD.Paid_id=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date) between p_begdate and p_enddate then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2),0) as v9_8a, --Первичная медико-санитарная помощь Сумма оплаченых поликлинических посещений за месяц 
coalesce(round((sum (case when rd.katnasel_sysnick like '%inog%' and rd.terrstrach=2 and RD.Paid_id=2 and rd.rtype in ('smp') and cast(rd.Registry_accDate as date) between ds  and p_enddate  then  coalesce(rd.RegistryData_ItogSum,0) end ))::numeric,2),0) as v9_9a --Первичная медико-санитарная помощь Сумма оплаченых поликлинических посещений с начала года
from registryAll rd 
