
			with mv as (
				select
					cast('2022-01-22' as date) as dt,
					cast('2022-01-22' as date) + interval '1 day' as tomdt
			),
			curDT as (select dbo.tzGetDate() as curDate)
			
		
			SELECT
				
				EvnSection.EvnSection_id as "EvnSection_id",
				EvnSection.EvnSection_rid as "EvnSection_rid",
				EvnSection.LpuSection_id as "LpuSection_id",
				Person.Sex_id as "Sex_id",
				case when 0=1
					then PEH.PersonEncrypHIV_Encryp
				end as "PersonEncrypHIV_Encryp",
				case when 0=1 and PEH.PersonEncrypHIV_id is not null
					then PEH.PersonEncrypHIV_Encryp
					else NULLIF(coalesce(Person.Person_SurName, '') || coalesce(' ' || Person.Person_FirName, '') || coalesce(' ' || Person.Person_SecName, ''), '')
				end as "Person_Fio",
				to_char(Person.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
				
            case 
                
                when datediff('day', Person.Person_Birthday, (select curDate from curDT)) <= 31
                    then '' || datediff('day', Person.Person_Birthday, (select curDate from curDT)) ||
                    case
                        when 
                            datediff('day', Person.Person_Birthday, (select curDate from curDT)) >= 2
                            and datediff('day', Person.Person_Birthday, (select curDate from curDT)) <= 4
                            or
                            datediff('day', Person.Person_Birthday, (select curDate from curDT)) >= 22
                            and datediff('day', Person.Person_Birthday, (select curDate from curDT)) + 1 <= 24
                        then ' дня'
                        when datediff('day', Person.Person_Birthday, (select curDate from curDT)) in (1, 21, 31)
                        then ' день'
                        else ' дней'
                    end
                when datediff('day', Person.Person_Birthday, (select curDate from curDT)) + 1 > 31 and dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT)) < 12
                    then '' || dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT))::varchar || ' мес.'
                    
                when dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT)) >= 12 and dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT)) < 36
                    then '' || dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar || 
                        case
                            when dbo.Age2(Person.Person_Birthday, (select curDate from curDT)) = 2 then ' года '
                            else ' год '
                        end ||
                        case
                            when (dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT)) - (dbo.Age2(Person.Person_Birthday, (select curDate from curDT)) * 12)) < 1 then ''
                            else (dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT)) - (dbo.Age2(Person.Person_Birthday, (select curDate from curDT)) * 12))::varchar || ' мес.'
                        end
                        
                when dbo.Age2(Person.Person_Birthday, (select curDate from curDT)) >= 3  
                    then '' || dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar || 
                        case
                    when 
                        right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,2)::integer >= 11 
                        and right(dbo.Age2(Person.Person_Birthday,(select curDate from curDT))::varchar,2)::integer <= 14 
                        or right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,1)::integer >= 5 
                        and right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,1)::integer <= 9 
                        or right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,1)::integer = 0 
                    then ' лет' 
                    when 
                        right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,1)::integer >= 2  
                        and right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,1)::integer <= 4 
                    then ' года' 
                    when right(dbo.Age2(Person.Person_Birthday, (select curDate from curDT))::varchar,1)::integer = 1 
                    then ' год' 
                end
                    else '' || dbo.Age_newborn(Person.Person_Birthday, (select curDate from curDT))::varchar || ' мес.' 
            end 
            as "Person_Age"
        ,
				dbo.Age_newborn(Person.Person_BirthDay, (select curDate from curDT)) as "Person_AgeMonth",
				coalesce(to_char(EvnSection.EvnSection_setDate, 'dd.mm.yyyy'), '') as "EvnSection_setDate",
				coalesce(to_char(EvnSection.EvnSection_disDate, 'dd.mm.yyyy'), '') as "EvnSection_disDate",
				EvnSection.Person_id as "Person_id",
				EvnSection.Server_id as "Server_id",
				EvnSection.PersonEvn_id as "PersonEvn_id",
				MedStaffFact.Person_Fin as "MedPersonal_Fin",
				case when exists(
					select *
					from v_PersonQuarantine PQ
					where PQ.Person_id = EvnSection.Person_id 
					and PQ.PersonQuarantine_endDT is null
				) then 2 else 1 end as "PersonQuarantine_IsOn",
				case when exists(
					select *
					from v_EvnSection ES
					inner join v_DiagFinance DF on DF.Diag_id = ES.Diag_id
					where ES.EvnSection_pid = EvnSection.EvnSection_pid
					and DF.DiagFinance_IsRankin = 2
				) then 2 else 1 end as "DiagFinance_IsRankin"
				
		,coalesce(to_char(EvnSection.EvnSection_PlanDisDT, 'dd.mm.yyyy'), '') as "EvnSection_PlanDisDT",
		Diag.Diag_Code as "Diag_Code",
		Diag.Diag_Name as "Diag_Name",
		EvnPS.EvnPS_NumCard as "EvnPS_NumCard",
		Mes.Mes_id as "Mes_id",
		Mes.Mes_Code as "Mes_Code",
		coalesce(Mes.Mes_KoikoDni, 0) as "KoikoDni",
		EvnPS.EvnPS_id as "EvnPS_id",
		bp.LpuSectionWard_id as "LpuSectionWard_id",
		EvnSection.MedPersonal_id as "MedPersonal_id",
		datediff('DAY', EvnSection.EvnSection_setDate, case when (EvnSection.EvnSection_disDate > (select curDate from curDT))
			then cast('2022-01-22' as date)
			else coalesce(EvnSection.EvnSection_disDate, cast('2022-01-22' as date))
		end) as "EvnSecdni",
		coalesce(ERP.EvnReanimatPeriod_id, 0) as "EvnReanimatPeriod_id"
		
			FROM
				v_EvnSection EvnSection
				
			left join lateral(
				Select
					*
				from v_Person_all Person
				where
					Person.Server_id = EvnSection.Server_id
					and Person.Person_id = EvnSection.Person_id
					and Person.PersonEvn_id = EvnSection.PersonEvn_id
				limit 1
			) Person on true
			
				
		left join v_Diag Diag on Diag.Diag_id = EvnSection.Diag_id
		left join v_EvnPS EvnPS on EvnPS.EvnPS_id = EvnSection.EvnSection_pid
		left join v_MesOld Mes on Mes.Mes_id = EvnSection.Mes_id
		left join v_LpuSectionWard LSW on LSW.LpuSectionWard_id = EvnSection.LpuSectionWard_id
			and LSW.LpuSection_id = EvnSection.LpuSection_id
		left join v_EvnReanimatPeriod ERP on ERP.EvnReanimatPeriod_pid = EvnSection.EvnSection_id
			and ERP.EvnReanimatPeriod_disDT is null
		
				LEFT JOIN v_MedStaffFact MedStaffFact on MedStaffFact.MedStaffFact_id = EvnSection.MedStaffFact_id
					and MedStaffFact.Lpu_id = EvnSection.Lpu_id --and MedStaffFact.LpuSection_id = EvnSection.LpuSection_id
				LEFT JOIN v_PersonEncrypHIV PEH on PEH.Person_id = Person.Person_id
				left join v_BedState bs on bs.EvnSection_id = EvnSection.EvnSection_id
						and bs.BedState_begDate <= (select curDate from curDT) and bs.BedState_endDate >= (select curDate from curDT)
				left join v_BedProperties bp on bp.Bed_id = bs.Bed_id and BedProperties_endDate is null
				
			WHERE
				
		EvnSection.Lpu_id = '13101283'
		and EvnSection.LpuSection_id = '99560030283'
		AND EvnPS.EvnPS_id is not null
		
				
					and cast(EvnSection.EvnSection_setDate as DATE) <= cast('2022-01-22' as DATE)
					and EvnSection.EvnSection_disDate is null
					 and bp.LpuSectionWard_id is not null and (bs.BedFondState_id = 3 or bs.BedFondState_id is null)
			ORDER BY
				EvnSection.EvnSection_setDate desc
		
