with Params as( 
select
	cast($1 as int8) as p_lpu_id, 
	cast($2 as int8) as p_lpuunit_id, 
	cast($3 as int8) as p_lpubuilding_id, 
	cast($4 as int8) as p_lpusection_id, 
	cast($5 as timestamp) as p_begdate, 
	cast($6 as timestamp) as p_enddate,
	cast($7 as int8) as p_yes_no_id
)
(SELECT 
	d.*,
	CASE WHEn d."LpuSection_name" = 'ИТОГО : ' then 3 else 1 end rownum
FROM
	(select 
		case when LpuSection.LpuSection_name is null 
			then ('ИТОГО ' || ': ' )
			else LpuSection.LpuSection_name 
		end as "LpuSection_name",
	max(LpuSection.LpuSection_id) as "LpuSection",
	
	count(distinct 
		case when p_lpusection_id is null 
			then 
				case when EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate then EvnPS_id end 
			else EvnPS_id 
		end) as v1, --vsego

	sum(case when v_LpuUnit.LpuUnitType_id = 1
			  then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
					case when 0 != 	DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
						 then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
						 else 1
						 end
			  else 
					 case when v_LpuUnit.LpuUnitType_id in (6,7,9)
						  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
						  else null
						  end 
			  end ) as v1_1 --as koikodni_vsego 
	   
	,round(cast(((sum(case when v_LpuUnit.LpuUnitType_id = 1
					  then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
							case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
								 then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
								 else 1
							end
					  else 
							case when v_LpuUnit.LpuUnitType_id in (6,7,9)
								 then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
								 else null
								 end 
					  end  )
			)/(count( coalesce( case when p_lpusection_id is null 
									then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
											  then EvnPS_id 
											  end 
									else EvnPS_id 
									end,1) ))) as numeric),0)
	as v1_2

	,count( distinct case when (v_EvnPS_rpt.Person_Age<1) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
																	or
																SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
																	or
																SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
						  then case when p_lpusection_id is null 
									then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
											  then EvnPS_id 
											  end 
									else EvnPS_id 
									end 
						  end) as v2

	,coalesce(sum(case 
	when (Person_Age<1) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
								or
							SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
								or
							SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then case when v_LpuUnit.LpuUnitType_id = 1
			  then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
				  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
					   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
					   else 1
					   end
				  else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
				    		then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							else null
							end 
				  end
			  end ),0) as v2_
			  
	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
															or
														SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v3

	,coalesce(sum(case 
	when (Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
												or
											SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1	
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v3_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v4

	,coalesce(sum(case 
	when (Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v4_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
															or
														SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v5

	,coalesce(sum(case 
	when (Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
												or
											SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v5_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v6

	,coalesce(sum(case 
	when (Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))----Организован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v6_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
															or
														SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v7

	,coalesce(sum(case 
	when (Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
												or
											SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v7_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v8

	,coalesce(sum(case 
	when (Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v8_


	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v9

	,coalesce(sum(case 
	when (Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v9_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v10

	,coalesce(sum(case 
	when (Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v10_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick = 'rab')--Работающий (включая раб. пенсионеров)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v11

	,coalesce(sum(case 
	when (Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick = 'rab')--Работающий (включая раб. пенсионеров)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v11_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('nrab') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
															or
														SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(10,91) 
															or
														SocStatus.SocStatus_SysNick in ('student') and SocStatus.region_id in(35,50))--Неработающий(не работает,не учится,не пенсионер)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v12

	,coalesce(sum(case 
	when (Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('nrab') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
												or
											SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(10,91) 
												or
											SocStatus.SocStatus_SysNick in ('student') and SocStatus.region_id in(35,50))--Неработающий(не работает,не учится,не пенсионер)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v12_
from 
	Params
	left join v_EvnSection on true --Движение в отделении
	inner join v_LpuSection LpuSection on (LpuSection.LpuSection_id = v_EvnSection.LpuSection_id)
		and ((LpuSection.LpuUnit_id=p_lpuunit_id) or (p_lpuunit_id is null))
		and ((LpuSection.LpuSection_id=p_lpusection_id) or (p_lpusection_id is null))
		and LpuSection.LpuSectionProfile_SysNick='profil'
	inner join diag on v_EvnSection.diag_id=diag.diag_id
	inner join v_LpuUnit on LpuSection.lpuUnit_id=v_LpuUnit.LpuUnit_id
		and (v_LpuUnit.LpuBuilding_id=p_lpubuilding_id or p_lpubuilding_id is null)
	--Карта выбывшего из стационара
	inner join v_EvnPS_rpt on (v_EvnPS_rpt.EvnPS_id = v_EvnSection.EvnSection_pid)
	left join v_Person_reg on (v_Person_reg.PersonEvn_id = v_EvnPS_rpt.PersonEvn_id) 
		and (v_Person_reg.server_id=v_EvnPS_rpt.server_id)
	left join SocStatus SocStatus on SocStatus.SocStatus_id = v_Person_reg.SocStatus_id
where 
	v_EvnSection.Lpu_id=p_lpu_id
	and v_EvnSection.EvnSection_disDate>=p_begdate
	and v_EvnPS_rpt.EvnPS_disDate<=p_enddate
group by rollup(LpuSection.LpuSection_name)
limit 100	
) d
union all
select 	
	'в т.ч. по койкам ' || lsbp.LpuSectionBedProfile_Name LpuSection_name,
	max(LpuSection.LpuSection_id) LpuSection,
	count(distinct case when p_lpusection_id is null 
				   then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
							 then EvnPS_id 
							 end 
				   else EvnPS_id 
				   end) as v1 --vsego

	,sum(case when v_LpuUnit.LpuUnitType_id = 1
			  then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
					case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
						 then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
						 else 1
						 end
			  else 
					 case when v_LpuUnit.LpuUnitType_id in (6,7,9)
						  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
						  else null
						  end 
			  end ) as v1_1 --as koikodni_vsego 
	   
	,round(cast (((sum(case when v_LpuUnit.LpuUnitType_id = 1
					  then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
							case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
								 then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
								 else 1
							end
					  else 
							case when v_LpuUnit.LpuUnitType_id in (6,7,9)
								 then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
								 else null
								 end 
					  end  )
			)/(count( coalesce( case when p_lpusection_id is null 
									then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
											  then EvnPS_id 
											  end 
									else EvnPS_id 
									end,1) ))) as numeric),0)
	as v1_2

	,count( distinct case when (v_EvnPS_rpt.Person_Age<1) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
																	or
																SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
																	or
																SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
						  then case when p_lpusection_id is null 
									then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
											  then EvnPS_id 
											  end 
									else EvnPS_id 
									end 
						  end) as v2

	,coalesce(sum(case 
	when (Person_Age<1) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
								or
							SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
								or
							SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then case when v_LpuUnit.LpuUnitType_id = 1
			  then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
				  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
					   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
					   else 1
					   end
				  else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
				    		then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							else null
							end 
				  end
			  end ),0) as v2_
			  
	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
															or
														SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v3

	,coalesce(sum(case 
	when (Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
												or
											SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v3_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v4

	,coalesce(sum(case 
	when (Person_Age between 1 and 2) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v4_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
															or
														SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v5

	,coalesce(sum(case 
	when (Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
												or
											SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v5_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v6

	,coalesce(sum(case 
	when (Person_Age between 3 and 5) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))----Организован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v6_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
															or
														SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v7

	,coalesce(sum(case 
	when (Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_doma') and SocStatus.region_id in(3,19,30,59)
												or
											SocStatus.SocStatus_SysNick in ('child') and SocStatus.region_id in(10,35,50,91)
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60))--Неорганизован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v7_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v8

	,coalesce(sum(case 
	when (Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(3,19,30,35,50,59))--Организован (дошкольник)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v8_
 

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v9

	,coalesce(sum(case 
	when (Person_Age between 6 and 14) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v9_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
															or
														SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v10

	,coalesce(sum(case 
	when (Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
												or
											SocStatus.SocStatus_SysNick in ('child_yasli') and SocStatus.region_id in(60) )--Учащийся(школьник,уч.спец.заведения,студент ВУЗа)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v10_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick = 'rab')--Работающий (включая раб. пенсионеров)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v11

	,coalesce(sum(case 
	when (Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick = 'rab')--Работающий (включая раб. пенсионеров)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v11_

	,count( distinct case 
	when (v_EvnPS_rpt.Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('nrab') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
															or
														SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(10,91) 
															or
														SocStatus.SocStatus_SysNick in ('student') and SocStatus.region_id in(35,50))--Неработающий(не работает,не учится,не пенсионер)
	then case when p_lpusection_id is null then case when  EvnSection_Index=EvnSection_Count-1 and EvnPS_disDate = EvnSection_disDate
	then EvnPS_id end else EvnPS_id end end) as v12

	,coalesce(sum(case 
	when (Person_Age between 15 and 17) and (SocStatus.SocStatus_SysNick in ('nrab') and SocStatus.region_id in(3,10,19,30,35,50,59,91,60) 
												or
											SocStatus.SocStatus_SysNick in ('study') and SocStatus.region_id in(10,91) 
												or
											SocStatus.SocStatus_SysNick in ('student') and SocStatus.region_id in(35,50))--Неработающий(не работает,не учится,не пенсионер)

	then  case when v_LpuUnit.LpuUnitType_id = 1
						then  --datediff дат поступления и выписки, но в случае совпадения дат равны 1
						  case when 0 != DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate)
							   then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) 
							   else 1
						   end
					else case when v_LpuUnit.LpuUnitType_id in (6,7,9)
							  then DATE_PART('day',v_EvnSection.EvnSection_disDate - v_EvnSection.EvnSection_setDate) + 1 --Для дневных стационаров и на дому - datediff дат поступления и выписки + 1.
							  else null
						 end 
				   end end ),0) as v12_,
	max(2) rownum
from
	Params
	left join v_EvnSection on true --Движение в отделении
	inner join v_LpuSection LpuSection on (LpuSection.LpuSection_id = v_EvnSection.LpuSection_id)
		and ((LpuSection.LpuUnit_id=p_lpuunit_id) or (p_lpuunit_id is null))
		and ((LpuSection.LpuSection_id=p_lpusection_id) or (p_lpusection_id is null))
		and LpuSection.LpuSectionProfile_SysNick='profil'
	inner join LpuSectionBedProfile lsbp on lsbp.LpuSectionBedProfile_id = v_EvnSection.LpuSectionBedProfile_id
	inner join diag on v_EvnSection.diag_id=diag.diag_id
	inner join v_LpuUnit on LpuSection.lpuUnit_id=v_LpuUnit.LpuUnit_id
		and (v_LpuUnit.LpuBuilding_id=p_lpubuilding_id or p_lpubuilding_id is null)
	--Карта выбывшего из стационара
	inner join v_EvnPS_rpt on (v_EvnPS_rpt.EvnPS_id = v_EvnSection.EvnSection_pid)
	left join v_Person_reg on (v_Person_reg.PersonEvn_id = v_EvnPS_rpt.PersonEvn_id) 
		and (v_Person_reg.server_id=v_EvnPS_rpt.server_id)
	left join SocStatus SocStatus on SocStatus.SocStatus_id = v_Person_reg.SocStatus_id
where 
	v_EvnSection.Lpu_id=p_lpu_id
	and v_EvnSection.EvnSection_disDate>=p_begdate
	and v_EvnPS_rpt.EvnPS_disDate<=p_enddate
	and p_yes_no_id = 2
group by lsbp.LpuSectionBedProfile_Name
limit 100)
order by "LpuSection",rownum
