with params as(
	select
		cast($1 as bigint) as p_Lpu_id,
		$2 as p_LpuFilialList,
		cast($3 as bigint) as p_lpuBuilding_id,
		cast($4 as bigint) as p_lpuunit_id,
		cast($5 as bigint) as p_lpuSection_id,
		cast($6 as bigint) as p_PayType_id,
		cast($7 as bigint) as p_MedPersonal_Id,
		cast($8 as timestamp) as p_begdate,
		cast($9 as timestamp) as p_enddate,
		coalesce(cast($10 as bigint),1) as p_IsGUZOnly,
		cast($11 as bigint) as p_region
),

L as(
	select
		L.Lpu_id,
		params.*
	from params
		inner join dbo.Lpu as L on L.Lpu_id = p_Lpu_id 
			or (p_Lpu_id is null
				and L.Lpu_id not in (100,101)
				and not(p_region = 59 and L.Lpu_id in (13002677, 13002714, 150000)))
		inner join dbo.Org as O on O.Org_id = L.Org_id
	where true
		and coalesce(L.Lpu_begDate::date, p_EndDate) <= p_EndDate
		and coalesce(L.Lpu_endDate::date, p_BegDate) >= p_BegDate
		and case when O.Okfs_id in (2,4,5) then 2 else 1 end >= p_IsGUZOnly
		and coalesce(L.Region_id, p_region) = p_region
),

usl as(
	select
		uc.UslugaComplex_id,
		uc.UslugaComplex_Code,
		case
			when uc.UslugaComplex_Code in(
				'A03.30.009','A03.04.001','A03.08.001.001','A03.08.002.002','A03.08.003','A03.08.003.001',
				'A03.08.004.001','A03.08.004.002','A03.08.004.003','A03.08.006','A03.08.007','A03.09.001',
				'A03.09.001.002','A03.09.002','A03.10.001','A03.11.001','A03.14.002','A03.15.001','A03.16.001',
				'A03.16.001.002','A03.16.001.003','A03.16.001.005','A03.16.003','A03.16.003.001','A03.17.001',
				'A03.17.001.001','A03.17.002','A03.17.002.001','A03.17.002.002','A03.18.001','A03.18.001.001',
				'A03.18.001.004','A03.18.001.006','A03.19.002','A03.19.003','A03.20.001','A03.20.002',
				'A03.20.003','A03.20.003.002','A03.20.003.003','A03.20.006','A03.25.005','A03.28.001',
				'A03.28.002','A03.28.003','A03.28.004','A03.30.006.001','A03.30.006.002','A03.30.006.003',
				'A03.30.006.004','A03.30.006.005','A03.30.006.006','A03.30.006.007','A03.30.008','A04.09.003',
				'A04.16.002','A04.16.003','A11.06.002.002','A11.08.008.001','A11.08.012.001','A11.08.016.001',
				'A11.09.005','A11.09.008','A11.09.010','A11.11.004.002','A11.14.003','A11.15.001',
				'A11.15.001.001','A11.16.001','A11.16.002','A11.16.003','A11.17.002','A11.18.001',
				'A11.19.001','A11.19.002','A11.19.009','A11.20.020','A11.23.005.001','A11.28.002.001',
				'A16.30.079','A16.30.080')
				then 1
			else 2
			end as part
	from params
		inner join dbo.UslugaComplex as uc on coalesce(uc.Region_id, p_region) = p_region
	where uc.UslugaComplex_Code in(
		'A03.04.001','A03.08.001.001','A03.08.002.002','A03.08.003','A03.08.003.001','A03.08.004.001',
		'A03.08.004.002','A03.08.004.003','A03.08.006','A03.08.007','A03.09.001','A03.09.001.001',
		'A03.09.001.002','A03.09.002','A03.10.001','A03.11.001','A03.14.002','A03.15.001','A03.16.001',
		'A03.16.001.001','A03.16.001.002','A03.16.001.003','A03.16.001.004','A03.16.001.005',
		'A03.16.003','A03.16.003.001','A03.17.001','A03.17.001.001','A03.17.002','A03.17.002.001',
		'A03.17.002.002','A03.18.001','A03.18.001.001','A03.18.001.004','A03.18.001.006',
		'A03.18.001.007','A03.19.002','A03.19.003','A03.19.004.001','A03.20.001','A03.20.002',
		'A03.20.003','A03.20.003.001','A03.20.003.002','A03.20.003.003','A03.20.006','A03.25.005',
		'A03.28.001','A03.28.001.001','A03.28.001.002','A03.28.002','A03.28.003','A03.28.004',
		'A03.30.006.001','A03.30.006.002','A03.30.006.003','A03.30.006.004','A03.30.006.005',
		'A03.30.006.006','A03.30.006.007','A03.30.008','A03.30.009','A04.09.003','A04.16.002',
		'A04.16.003','A11.06.002.002','A11.08.008.001','A11.08.012.001','A11.08.016.001','A11.09.005',
		'A11.09.008','A11.09.009','A11.09.010','A11.11.004.002','A11.14.003','A11.15.001',
		'A11.15.001.001','A11.16.001','A11.16.002','A11.16.003','A11.17.002','A11.18.001','A11.19.001',
		'A11.19.002','A11.19.009','A11.20.020','A11.20.036','A11.23.005.001','A11.28.002.001',
		'A16.04.003.001','A16.07.081.004','A16.08.007','A16.08.024.001','A16.08.043','A16.09.005.001',
		'A16.09.012','A16.09.014.007','A16.09.030','A16.10.009.001','A16.10.011.004','A16.10.011.005',
		'A16.10.019.008','A16.12.020.001','A16.12.020.002','A16.14.008','A16.14.032.002',
		'A16.14.032.003','A16.14.032.004','A16.14.032.005','A16.14.043','A16.15.021','A16.15.022',
		'A16.15.022.001','A16.16.006.002','A16.16.017.016','A16.16.018.005','A16.16.032.002',
		'A16.16.038','A16.16.038.001','A16.16.039','A16.16.040.001','A16.16.041.001','A16.16.041.002',
		'A16.16.041.003','A16.16.047','A16.16.047.001','A16.16.048','A16.16.057','A16.16.064.001',
		'A16.16.065.001','A16.16.066','A16.16.067','A16.17.018','A16.17.020','A16.17.023',
		'A16.18.019.001','A16.18.025','A16.18.026','A16.18.027','A16.18.032','A16.19.017','A16.19.018',
		'A16.20.008','A16.20.009','A16.20.035.001','A16.20.059','A16.20.059.001','A16.20.077',
		'A16.22.014.005','A16.22.014.006','A16.23.024.002','A16.23.032.005','A16.23.048.002',
		'A16.23.052.004','A16.23.068.003','A16.23.069.003','A16.28.013','A16.28.013.001',
		'A16.28.013.002','A16.28.030.002','A16.28.030.004','A16.28.030.005','A16.28.031.002',
		'A16.28.031.004','A16.28.031.005','A16.28.045.001','A16.28.045.002','A16.28.083',
		'A16.30.025.004','A16.30.037','A16.30.042.001','A16.30.074','A16.30.079','A16.30.080',
		'A22.08.008','A22.08.009','A22.08.009.004','A22.08.017','A22.08.018','A22.08.019','A22.08.020',
		'A22.08.021','A22.08.022','A22.09.002','A22.09.003','A22.09.003.003','A22.09.003.007',
		'A22.09.013','A22.09.014','A22.12.006','A22.16.007','A22.16.008','A22.16.009','A22.16.010',
		'A22.16.011','A22.16.012','A22.17.002','A22.17.003','A22.17.004','A22.18.002.002','A22.18.003',
		'A22.18.004','A22.19.005.004','A22.19.005.005','A22.19.005.006','A22.19.006','A22.19.007',
		'A22.19.008','A22.19.009','A22.19.010','A22.19.011','A22.25.005','A22.25.006','A22.25.007',
		'A16.30.074','A16.30.74'
	)
),

rd as(
	select
		t_table.part,
		t_table.ord,
		t_table.prime_num,
		t_table.v1,
		t_table.v2
	from(values
		(1,1 ,1 ,'Эндоскопические исследования диагностические - всего' ,'1'),
		(1,2 ,2 ,'из них:'||chr(10)||'в подразделениях, оказывающих медицинскую помощь в амбулаторных условиях','2'),
		(1,3 ,3 ,'в условиях дневного стационара','3'),
		(1,4 ,5 ,'выполненных под анестезией','4'),
		(1,5 ,7 ,'со взятием биопсии','5'),
		(1,6 ,11,'выполненных по экстренным показаниям','6'),
		(1,7 ,13,'с увеличением','7'),
		
		(2,8 ,1 ,'Эндоскопические лечебные манипуляции – всего','8'),
		(2,9 ,2 ,'из них:'||chr(10)||'в подразделениях, оказывающих медицинскую помощь в амбулаторных условиях','9'),
		(2,10,3 ,'в условиях дневного стационара','10'),
		(2,11,5 ,'выполненных под анестезией','11'),
		(2,12,1 ,'удаление доброкачественных новообразований','12'),
		(2,13,17,'из них  диссекцией в подслизистом слое','12.1'),
		(2,14,19,'удаление инородных тел','13'),
		(2,15,23,'баллонная дилатация','14'),
		(2,16,1 ,'стентирование','15'),
		(2,17,1 ,'остановка кровотечений, всего','16'),
		(2,18,1 ,'из них:'||chr(10)||'по экстренным показаниям','16.1'),
		(2,19,1 ,'с применением клипс','16.2')
	)as t_table(part,ord,prime_num,v1,v2) 
),

evn_usl as(
	select 
		eu.Evn_id as EvnUsluga_id,
		uc.UslugaComplex_Code,
		uc.part,
		evns.Diag_Code,
		evns.osl_Diag_Code,
		evns.has_eps,
		sub.prime_code
	from L
		inner join dbo.EvnUsluga as eu on eu.Lpu_id = L.lpu_id
		inner join dbo.UslugaComplex as p_uc
			on p_uc.UslugaComplex_id = eu.UslugaComplex_id
		inner join usl as uc on uc.Uslugacomplex_id = p_uc.UslugaComplex_2011id
		left join lateral(
			select
				eps.Evn_id,
				true as has_eps,
				false as has_epl,
				case when p_region = 59 then d.Diag_Code else d.Diag_SCode end as Diag_Code,
				edps.osl_Diag_Code
			from dbo.EvnPS as eps
				left join dbo.Diag as d on d.Diag_id = eps.Diag_id
				left join lateral(
					select case when p_region = 59 then d_s.Diag_Code else d_s.Diag_SCode end as osl_Diag_Code
					from dbo.EvnDiagPS as edps
						inner join dbo.Diag as d_s on d_s.Diag_id = edps.Diag_id
					where edps.Evn_rid = eps.Evn_id
						and edps.EvnClass_id = 33
						and edps.Evn_deleted = 1
						and edps.DiagSetClass_id = 2
				)edps on true
			where eps.Evn_id = eu.Evn_rid
				and eps.EvnClass_id = 30
				and eps.Evn_deleted = 1

			union all select
				epl.Evn_id,
				false as has_eps,
				true as has_epl,
				case when p_region = 59 then d.Diag_Code else d.Diag_SCode end as Diag_Code,
				null as osl_Diag_Code
			from dbo.EvnPL as epl
				left join dbo.Diag as d on d.Diag_id = epl.Diag_id
			where epl.Evn_id = eu.Evn_rid
				and epl.EvnClass_id in (3,6,35,60)
				and epl.Evn_deleted = 1
		)evns on true
		left join dbo.lpusection as ls on ls.lpusection_id = eu.LpuSection_uid
		left join dbo.lpuunit as lu on lu.LpuUnit_id = ls.LpuUnit_id
		left join dbo.LpuBuilding as lb on lb.LpuBuilding_id = lu.LpuBuilding_id

		left join lateral(
			select 
				case
					when exists(
						select 1
						from dbo.EvnUsluga as eu_an
							inner join dbo.UslugaComplex as p_uc
								on p_uc.UslugaComplex_id = eu_an.UslugaComplex_id
							inner join dbo.UslugaComplex as uc_an
								on uc_an.UslugaComplex_id = p_uc.UslugaComplex_2011id
						where eu_an.Person_id = eu.Person_id
							and eu_an.Evn_setDT::date = eu.Evn_setDT::date
							and uc_an.UslugaComplex_Code in(
								'B01.003.004.001','B01.003.004.002','B01.003.004.003',
								'B01.003.004.005','B01.003.004.006','B01.003.004.007',
								'B01.003.004.008','B01.003.004.009','B01.003.004.010',
								'B01.003.004.011','B01.003.004.012'))
						then 5
					else 1 end
					* case
						when evns.has_eps and lu.LpuUnitType_id in (6,9)
							then 3
						else 1 end
					* case
						when evns.has_epl
							then 2
						else 1 end
					* case
						when uc.UslugaComplex_Code in('A11.16.001','A11.16.002','A11.16.003','A11.18.001',
								'A11.08.012.001','A11.09.008','A11.09.010','A11.19.001','A11.19.002',
								'A11.17.002','A11.08.008.001','A11.08.016.001','A11.11.004.002','A11.14.003',
								'A11.15.001','A11.15.001.001','A11.19.009','A11.20.020','A11.23.005.001',
								'A11.28.002.001')
							then 7
						else 1 end
					* case
						when eu.EvnUsluga_isCito = 2
							then 11
						when exists(
							select 1
							from dbo.EvnDirection as ed
							where ed.Evn_id = eu.EvnDirection_id
								and ed.EvnDirection_IsCito = 2
								and ed.Evn_deleted = 1
						)
							then 11
						else 1 end
					* case
						when uc.UslugaComplex_Code in ('A03.30.006.002')
							then 13
						else 1 end
					* case
						when uc.UslugaComplex_Code in ('A16.16.038.001','A16.16.041.001')
							then 17
						else 1 end
					* case
						when uc.UslugaComplex_Code in ('A16.16.041.003','A16.16.048','A16.18.032','A16.09.012',
								'A16.17.023','A16.04.003.001','A16.08.007','A16.14.008','A16.19.018',
								'A16.20.059','A16.28.013','A16.28.013.001','A16.28.013.002','A16.30.025.004')
							then 19
						else 1 end
					* case
						when uc.UslugaComplex_Code in ('A16.16.032.002','A16.16.047','A16.16.057','A16.20.077',
								'A16.14.043','A16.23.052.004','A16.28.083')
							then 23
						else 1 end as prime_code
		)sub on true
	where true
		and eu.Evn_setDT between p_begdate and p_enddate + interval'23hours 59minute'
		and eu.EvnClass_id in (21,22,23,24,43,47,100,197)
		and eu.Evn_deleted = 1
		and (eu.MedPersonal_id = p_MedPersonal_id or p_MedPersonal_id is null)
		and (eu.PayType_id = p_PayType_id or p_PayType_id is null)
		and (eu.EvnClass_id != 43 --EvnUslugaOper
			or exists(
				select 1
				from dbo.EvnUslugaOper as euo
				where euo.Evn_id = eu.Evn_id
					and euo.EvnUslugaOper_IsEndoskop = 2
					and coalesce(eu.Evn_deleted,1) = 1))
		and (ls.LpuSection_id = p_lpusection_id or p_LpuSection_id is null)
		and (lu.lpuunit_id = p_lpuunit_id or p_lpuunit_id is null)
		and (lu.lpuBuilding_id = p_lpuBuilding_id or p_lpuBuilding_id is null)
		and (lb.LpuFilial_id = any(('{'||p_LpuFilialList||'}')::int8[])
			or coalesce(rtrim(p_LpuFilialList), '') = '')
)

select
	rd.ord,
	rd.v1,
	rd.v2,
	count(distinct
		case
			when rd.part = 1 then 
				case
					when usl.UslugaComplex_Code in (
							'A03.08.003','A03.08.003.001','A03.16.001','A03.16.001.002','A03.16.001.003',
							'A03.16.001.005','A03.16.003','A03.16.003.001',' A03.30.009','A04.16.002',
							'A04.16.003','A11.16.001','A11.16.002','A11.16.003')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in (
							'A03.30.006.002','A03.30.006.003','A03.30.006.004','A03.30.006.006','A03.30.006.007')
							and (left(usl.Diag_Code, 3) between 'K20' and 'K31'
								or left(usl.Diag_Code, 3) between 'C15' and 'C16'
								or usl.Diag_Code between 'D13.0' and 'D13.2'
								or usl.Diag_Code in ('C17.0','D00.1','D00.2')) 
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 12 then
				case
					when usl.UslugaComplex_Code in('A16.16.038.001','A16.16.041.001','A16.16.041.002')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in(
							'A16.16.038','A16.16.039','A22.16.008','A22.16.010','A22.16.012')
							and usl.Diag_Code = 'K31.7'
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 16 then
				case
					when usl.UslugaComplex_Code in ('A16.16.006.002')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and (left(usl.Diag_Code,3) in ('C15', 'C16')
								or usl.Diag_Code = 'C17.0')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord in(17,18) then
				case
					when usl.UslugaComplex_Code in('A03.16.001.001')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and (left(usl.Diag_Code,3) in ('C15','C16')
								or usl.Diag_Code in('C17.0','K22.1','K22.6','K22.8','K25.0','K25.4',
									'K26.0','K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and (left(usl.osl_Diag_Code,3) in ('C15','C16')
								or usl.osl_Diag_Code in('C17.0','K22.1','K22.6','K22.8','K25.0','K25.4',
									'K26.0','K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0'))
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 19 then
				case
					when usl.UslugaComplex_Code in('A16.30.074')
							and (left(usl.Diag_Code,3) in ('C15','C16')
								or usl.Diag_Code in('C17.0','K22.1','K22.6','K22.8','K25.0','K25.4','K26.0',
									'K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.30.074')
							and (left(usl.osl_Diag_Code,3) in ('C15','C16')
								or usl.osl_Diag_Code in('C17.0','K22.1','K22.6','K22.8','K25.0','K25.4','K26.0',
									'K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0'))
						then EvnUsluga_id
					end
			else
				case
					when usl.UslugaComplex_Code in (
							'A03.16.001.001','A03.16.001.004','A16.16.006.002','A16.16.032.002','A16.16.038',
							'A16.16.038.001','A16.16.039','A16.16.041.001','A16.16.041.002','A16.16.041.003',
							'A16.16.047','A16.16.047.001','A16.16.048','A16.16.057','A22.16.007','A22.16.008',
							'A22.16.009','A22.16.010','A22.16.011','A22.16.012')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A16.30.074','A22.12.006')
							and left(usl.Diag_Code,3) in ('C15','C16')
							or usl.Diag_Code in ('C17.0','K25.0','K25.4','K26.0','K26.4','K27.0','K27.4',
								'K28.0','K28.4','K92.0','K92.1')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and (left(usl.Diag_Code,3) in ('C15', 'C16')
								or usl.Diag_Code = 'C17.0')
						then EvnUsluga_id
					end
			end) as v4,
	count(distinct
		case
			when rd.ord in (13,15) then null
			when rd.part = 1 then 
				case
					when usl.UslugaComplex_Code in('A03.18.001','A03.18.001.001','A03.18.001.004','A11.18.001')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in (
							'A03.30.006.002','A03.30.006.003','A03.30.006.004','A03.30.006.006','A03.30.006.007')
							and (left(usl.Diag_Code, 3) in('K50','K51','K52','K56','K57','K63','C18')
								or usl.Diag_Code between 'D12.0' and 'D12.6'
								or usl.Diag_Code in ('C78.5','D01.0')) 
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 12 then
				case
					when usl.UslugaComplex_Code in('A16.18.019.001')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A22.18.002.002','A22.18.003','A22.18.004')
							and usl.Diag_Code = 'K63.5'
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 16 then
				case
					when usl.UslugaComplex_Code in ('A16.30.037') and left(usl.Diag_Code,3) = 'C18'
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord in(17,18) then
				case
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and (left(usl.Diag_Code,3) in('C18','K51','K52','K55','K57')
								or usl.Diag_Code in('K63.3','K63.5','K92.1','K92.2'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and (left(usl.osl_Diag_Code,3) in('C18','K51','K52','K55','K57')
								or usl.osl_Diag_Code in('K63.3','K63.5','K92.1','K92.2'))
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 19 then
				case
					when usl.UslugaComplex_Code in('A16.30.074')
							and (left(usl.Diag_Code,3) in ('C18','K51','K52','K55','K57')
								or usl.Diag_Code in ('K63.3','K63.5','K92.1','K92.2'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.30.074')
							and (left(usl.osl_Diag_Code,3) in ('C18','K51','K52','K55','K57')
								or usl.osl_Diag_Code in ('K63.3','K63.5','K92.1','K92.2'))
						then EvnUsluga_id
					end
			else
				case
					when usl.UslugaComplex_Code in('A03.18.001.007','A16.18.019.001','A16.18.025','A16.18.027',
							'A16.18.032','A22.18.002.002','A22.18.003','A22.18.004')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A16.30.074','A22.12.006')
							and (left(usl.Diag_Code,3) in ('C18','K51','K52','K55','K57')
								or usl.Diag_Code in('K63.3','K63.5','K92.2'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037') and left(usl.Diag_Code,3) = 'C18'
						then EvnUsluga_id
					end
			end) as v5,
	count(distinct
		case
			when rd.ord in (7,12,13,15,19) then null
			when rd.part = 1 then 
				case
					when usl.UslugaComplex_Code in('A03.09.001','A03.09.001.002','A03.09.002','A03.30.006.001',
							'A03.30.006.005','A04.09.003','A11.08.012.001','A11.09.005','A11.09.008','A11.09.010')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 16 then
				case
					when usl.UslugaComplex_Code in ('A16.08.043','A16.09.030')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and left(usl.Diag_Code,3) in ('C33','C34')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord in(17,18) then
				case
					when usl.UslugaComplex_Code in('A16.09.005.001')
						then EvnUsluga_id
					end
			else
				case
					when usl.UslugaComplex_Code in('A03.09.001.001','A11.09.009','A16.08.043','A16.09.005.001',
							'A16.09.012','A16.09.030','A22.08.009','A22.08.009.004','A22.09.002','A22.09.003',
							'A22.09.003.003','A22.09.003.007')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and left(usl.Diag_Code,3) in ('C33','C34')
						then EvnUsluga_id
					end
			end) as v6,
	count(distinct
		case
			when rd.ord in (13,14,15) then null
			when rd.part = 1 then 
				case
					when usl.UslugaComplex_Code in('A03.19.002','A03.19.003','A11.19.001','A11.19.002')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in (
							'A03.30.006.002','A03.30.006.003','A03.30.006.004','A03.30.006.006','A03.30.006.007')
							and (left(usl.Diag_Code, 3) between 'K60' and 'K62'
								or left(usl.Diag_Code, 3) between 'C19' and 'C21'
								or usl.Diag_Code between 'D01.1' and 'D01.3'
								or usl.Diag_Code between 'D12.7' and 'D12.9') 
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 12 then
				case
					when usl.UslugaComplex_Code in('A16.19.017')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A22.19.005.004','A22.19.005.005','A22.19.005.006',
							'A22.19.006','A22.19.007','A22.19.008','A22.19.009','A22.19.010','A22.19.011')
							and usl.Diag_Code in('K62.0','K62.1')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 16 then
				case
					when usl.UslugaComplex_Code in ('A16.30.037')
							and left(usl.Diag_Code,3) in ('C19','C20')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord in(17,18) then
				case
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and (left(usl.Diag_Code,3) in ('C19','C20','K60','K61')
								or usl.Diag_Code in('K62.5'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and (left(usl.osl_Diag_Code,3) in ('C19','C20','K60','K61')
								or usl.osl_Diag_Code in('K62.5'))
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 19 then
				case
					when usl.UslugaComplex_Code in('A16.30.074')
							and (left(usl.Diag_Code,3) in ('C19','C20','K60','K61')
								or usl.Diag_Code in ('K62.5'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.30.074')
							and (left(usl.osl_Diag_Code,3) in ('C19','C20','K60','K61')
								or usl.osl_Diag_Code in ('K62.5'))
						then EvnUsluga_id
					end
			else
				case
					when usl.UslugaComplex_Code in('A16.19.017','A16.19.018','A03.19.004.001',
							'A16.12.020.001','A16.12.020.002','A22.12.006','A22.19.005.004','A22.19.005.005',
							'A22.19.005.006','A22.19.006','A22.19.007','A22.19.008','A22.19.009','A22.19.010',
							'A22.19.011')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A16.30.074','A22.12.006')
							and (left(usl.Diag_Code,3) in ('C19', 'C20', 'K60','K61')
								or usl.Diag_Code in('K62.5'))
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and left(usl.Diag_Code,3) in ('C19','C20')
						then EvnUsluga_id
					end
			end) as v7,
	count(distinct
		case
			when rd.ord in (7,13,15,17,18,19) then null
			when rd.part = 1 then 
				case
					when usl.UslugaComplex_Code in(
							'A03.17.001','A03.17.001.001','A03.17.002','A03.17.002.001','A11.17.002')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 12 then
				case
					when usl.UslugaComplex_Code in('A16.17.020')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 16 then
				case
					when usl.UslugaComplex_Code in ('A16.30.037')
							and usl.Diag_Code between 'C17.1' and 'C17.9'
						then EvnUsluga_id
					end
			else
				case
					when usl.UslugaComplex_Code in('A16.17.018','A16.17.020','A16.17.023','A22.17.002',
							'A22.17.003','A22.17.004')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and usl.Diag_Code between 'C17.1' and 'C17.9'
						then EvnUsluga_id
					end
			end) as v8,
	count(distinct
		case
			when rd.part = 1 and rd.ord not in (5,7) then 
				case
					when usl.UslugaComplex_Code in('A03.17.002.002','A03.18.001.006')
						then EvnUsluga_id
					end
			else null
			end) as v9,
	count(distinct
		case
			when rd.ord in (7,13) then null
			when rd.part = 1 then 
				case
					when usl.UslugaComplex_Code in(
							'A03.04.001','A03.08.001.001','A03.08.002.002','A03.08.004.001','A03.08.004.002',
							'A03.08.004.003','A03.08.006','A03.08.007','A03.10.001','A03.11.001','A03.14.002',
							'A03.15.001','A03.20.001','A03.20.002','A03.20.003','A03.20.003.002',
							'A03.20.003.003','A03.20.006','A03.25.005','A03.28.001','A03.28.002','A03.28.003',
							'A03.28.004','A03.30.008','A11.06.002.002','A11.08.008.001','A11.08.016.001',
							'A11.11.004.002','A11.14.003','A11.15.001','A11.15.001.001','A11.19.009',
							'A11.20.020','A11.23.005.001','A11.28.002.001','A16.30.079','A16.30.080')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in (
							'A03.30.006.002','A03.30.006.003','A03.30.006.004','A03.30.006.006','A03.30.006.007')
							and left(usl.Diag_Code, 3) not between 'K20' and 'K31'
							and left(usl.Diag_Code, 3) not between 'C15' and 'C16'
							and usl.Diag_Code not between 'D13.0' and 'D13.2'
							and usl.Diag_Code not in ('C17.0','D00.1','D00.2','C78.5','D01.0')
							and left(usl.Diag_Code, 3) not in('K50','K51','K52','K56','K57','K63','C18')
							and usl.Diag_Code not between 'D12.0' and 'D12.6'
							and left(usl.Diag_Code, 3) not between 'K60' and 'K62'
							and left(usl.Diag_Code, 3) not between 'C19' and 'C21'
							and usl.Diag_Code not between 'D01.1' and 'D01.3'
							and usl.Diag_Code not between 'D12.7' and 'D12.9'
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 12 then
				case
					when usl.Diag_Code = 'H74.4'
							and usl.UslugaComplex_Code in('A22.25.005','A22.25.006','A22.25.007')
						then EvnUsluga_id
					when usl.Diag_Code = 'J33.0' and usl.UslugaComplex_Code in('A22.08.017','A22.08.020')
						then EvnUsluga_id
					when usl.Diag_Code in ('J33.1','J33.8')
							and usl.UslugaComplex_Code in('A22.08.018','A22.08.021')
						then EvnUsluga_id
					when usl.Diag_Code = 'J38.1'
							and usl.UslugaComplex_Code in('A22.08.008','A22.08.019','A22.08.022')
						then EvnUsluga_id
					when left(usl.Diag_Code,3) = 'N84' and usl.UslugaComplex_Code in('A03.20.003.001')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 16 then
				case
					when usl.UslugaComplex_Code in('A16.14.032.002','A16.14.032.003','A16.14.032.004',
							'A16.14.032.005','A16.15.022','A16.15.021','A16.15.022.001')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and left(usl.Diag_Code,1) = 'C'
							and left(usl.Diag_Code,3) not in(
								'C15', 'C16', 'C17','C18','C33','C34','C19','C20')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord in(17,18) then
				case
					when usl.UslugaComplex_Code in('A16.30.042.001')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A22.12.006','A16.30.074')
							and coalesce(left(usl.Diag_Code,3),'') not in (
								'C15','C16','C18','K51','K52','K55','K57','C19','C20','K60','K61')
							and coalesce(usl.Diag_Code,'') not in(
								'C17.0','K22.1','K22.6','K22.8','K25.0','K25.4',
								'K26.0','K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0',
								'K63.3','K63.5','K92.1','K92.2','K62.5')
							and coalesce(left(usl.osl_Diag_Code,3),'') not in (
								'C15','C16','C18','K51','K52','K55','K57','C19','C20','K60','K61')
							and coalesce(usl.osl_Diag_Code,'') not in(
								'C17.0','K22.1','K22.6','K22.8','K25.0','K25.4',
								'K26.0','K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0',
								'K63.3','K63.5','K92.1','K92.2','K62.5')
						then EvnUsluga_id
					end
			when rd.part = 2 and rd.ord = 19 then
				case
					when usl.UslugaComplex_Code in('A16.30.074')
							and coalesce(left(usl.Diag_Code,3),'') not in (
								'C15','C16','C18','K51','K52','K55','K57','C19','C20','K60','K61')
							and coalesce(usl.Diag_Code,'') not in(
								'C17.0','K22.1','K22.6','K22.8','K25.0','K25.4','K26.0',
								'K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0','K63.3',
								'K63.5','K92.1','K92.2','K62.5')
							and coalesce(left(usl.osl_Diag_Code,3),'') not in (
								'C15','C16','C18','K51','K52','K55','K57','C19','C20','K60','K61')
							and coalesce(usl.osl_Diag_Code,'') not in(
								'C17.0','K22.1','K22.6','K22.8','K25.0','K25.4','K26.0',
								'K26.4','K27.0','K27.4','K28.0','K28.4','K92.0','K92.1','I85.0','K63.3',
								'K63.5','K92.1','K92.2','K62.5')
						then EvnUsluga_id
					end
			else
				case
					when usl.UslugaComplex_Code in(
							'A03.20.003.001','A03.28.001.001','A03.28.001.002','A11.20.036','A16.04.003.001',
							'A16.07.081.004','A16.08.007','A16.08.024.001','A16.09.014.007','A16.10.009.001',
							'A16.10.011.004','A16.10.011.005','A16.10.019.008',
							'A16.14.008','A16.14.032.002','A16.14.032.003','A16.14.032.004','A16.14.032.005',
							'A16.14.043','A16.15.022','A16.15.021','A16.15.022.001','A16.16.017.016',
							'A16.16.018.005','A16.16.040.001','A16.16.064.001','A16.16.065.001','A16.16.066',
							'A16.16.067','A16.18.026','A16.20.008','A16.20.009','A16.20.035.001','A16.20.059',
							'A16.20.059.001','A16.20.077','A16.22.014.005','A16.22.014.006','A16.23.024.002',
							'A16.23.032.005','A16.23.048.002','A16.23.052.004','A16.23.068.003','A16.23.069.003',
							'A16.28.013','A16.28.013.001','A16.28.013.002','A16.28.030.002','A16.28.030.004',
							'A16.28.030.005','A16.28.031.002','A16.28.031.004','A16.28.031.005','A16.28.045.001',
							'A16.28.045.002','A16.28.083','A16.30.025.004','A16.30.042.001',
							'A22.08.008','A22.08.017','A22.08.018','A22.08.019','A22.08.020','A22.08.021',
							'A22.08.022','A22.09.013','A22.09.014','A22.25.005','A22.25.006',
							'A22.25.007')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in('A16.12.020.001','A16.12.020.002','A16.30.074','A22.12.006')
							and left(usl.Diag_Code,3) not in (
								'C15','C16','C18','K51','K52','K55','K57','C19', 'C20', 'K60','K61')
							and usl.Diag_Code not in ('C17.0','K25.0','K25.4','K26.0','K26.4','K27.0','K27.4',
								'K28.0','K28.4','K92.0','K92.1','K63.3','K63.5','K92.2','K62.5')
						then EvnUsluga_id
					when usl.UslugaComplex_Code in ('A16.30.037')
							and left(usl.Diag_Code,1) = 'C'
							and left(usl.Diag_Code,3) not in(
								'C15', 'C16', 'C17','C18','C33','C34','C19','C20')
						then EvnUsluga_id
					end
			end) as v10
from evn_usl as usl
	right join rd on usl.prime_code % rd.prime_num = 0
		and rd.part = usl.part
group by
	rd.ord,
	rd.v1,
	rd.v2
order by rd.ord
