
			with mv as (
				select
					cast('2022-01-17' as date) as dt,
					cast('2022-01-17' as date) + interval '1 day' as tomdt,
					dbo.tzGetDate() - interval '1 day' as yesdt,
					dbo.tzGetDate() as curdt
			),
			curDT as (select  cast('2022-01-17' as date) as curDate),
			other (
						EvnSection_id
				) as (
					select 
						t1.EvnOtherSection_pid as "EvnSection_id"
					from v_EvnOtherSection t1 
					where 
						LpuSection_oid = '99560027549'
				union all
					select 
						t1.EvnOtherSectionBedProfile_pid as "EvnSection_id"
					from v_EvnOtherSectionBedProfile t1 
					where 
						LpuSection_oid = '99560027549'
				union all
					select 
						t1.EvnOtherStac_pid as "EvnSection_id"
					from v_EvnOtherStac t1 
					where 
						LpuSection_oid = '99560027549'
			)
			select
				EvnSection.EvnSection_id as "EvnSection_id",
				EvnSection.EvnSection_rid as "EvnSection_rid",
				EvnSection.LpuSection_id as "LpuSection_id",
				LS.LpuSection_Name as "LpuSection_Name",
				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 
						case 
							when (Person.Person_SurName is null and Person.Person_FirName is null and Person.Person_SecName is null) 
							then 'Неизвестный'
							else 
								coalesce(upper(left(Person.Person_SurName, 1)) || lower(overlay(Person.Person_SurName placing '' from 1 for 1)),'') ||' '|| 
								coalesce(upper(left(Person.Person_FirName, 1)) || lower(overlay(Person.Person_FirName placing '' from 1 for 1)),'') ||' '|| 
								coalesce(upper(left(Person.Person_SecName, 1)) || lower(overlay(Person.Person_SecName placing '' from 1 for 1)),'')
						end
				end as "Person_Fio",
				to_char(Person.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
				
            case 
                when PEH.PersonEncrypHIV_Encryp is not null then null
                when datediff('day', Person.Person_Birthday, (select curdt from mv)) <= 31
                    then '' || datediff('day', Person.Person_Birthday, (select curdt from mv)) ||
                    case
                        when 
                            datediff('day', Person.Person_Birthday, (select curdt from mv)) >= 2
                            and datediff('day', Person.Person_Birthday, (select curdt from mv)) <= 4
                            or
                            datediff('day', Person.Person_Birthday, (select curdt from mv)) >= 22
                            and datediff('day', Person.Person_Birthday, (select curdt from mv)) + 1 <= 24
                        then ' дня'
                        when datediff('day', Person.Person_Birthday, (select curdt from mv)) in (1, 21, 31)
                        then ' день'
                        else ' дней'
                    end
                when datediff('day', Person.Person_Birthday, (select curdt from mv)) + 1 > 31 and dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv)) < 12
                    then '' || dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv))::varchar || ' мес.'
                    
                when dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv)) >= 12 and dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv)) < 36
                    then '' || dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar || 
                        case
                            when dbo.Age2(Person.Person_Birthday, (select curdt from mv)) = 2 then ' года '
                            else ' год '
                        end ||
                        case
                            when (dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv)) - (dbo.Age2(Person.Person_Birthday, (select curdt from mv)) * 12)) < 1 then ''
                            else (dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv)) - (dbo.Age2(Person.Person_Birthday, (select curdt from mv)) * 12))::varchar || ' мес.'
                        end
                        
                when dbo.Age2(Person.Person_Birthday, (select curdt from mv)) >= 3  
                    then '' || dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar || 
                        case
                    when 
                        right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,2)::integer >= 11 
                        and right(dbo.Age2(Person.Person_Birthday,(select curdt from mv))::varchar,2)::integer <= 14 
                        or right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,1)::integer >= 5 
                        and right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,1)::integer <= 9 
                        or right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,1)::integer = 0 
                    then ' лет' 
                    when 
                        right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,1)::integer >= 2  
                        and right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,1)::integer <= 4 
                    then ' года' 
                    when right(dbo.Age2(Person.Person_Birthday, (select curdt from mv))::varchar,1)::integer = 1 
                    then ' год' 
                end
                    else '' || dbo.Age_newborn(Person.Person_Birthday, (select curdt from mv))::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",
				datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar ||
				case 
					when (
						right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,2)::integer >= 11 
						and right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,2)::integer <= 14 
						or right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,1)::integer >= 5 
						and right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,1)::integer <= 9 
						or right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,1)::integer = 0
					) then ' дней' 
					when (
						right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,1)::integer >= 2  
						and right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,1)::integer <= 4
					) then ' дня' 
					when (
						right(datediff('day', EvnSection.EvnSection_setDate, (select curDate from curDT))::varchar,1)::integer = 1
					) then ' день'
				end as "InEvnSection_date",
				EvnSection.Person_id as "Person_id",
				EvnSection.Server_id as "Server_id",
				EvnSection.PersonEvn_id as "PersonEvn_id",
				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",
				MP.Person_Fin as "MedPersonal_Fin",
				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",
				EvnSection.MedPersonal_id as "MedPersonal_id",
				datediff(
					'day', 
					EvnSection.EvnSection_setDate, 
					case 
						when EvnSection.EvnSection_disDate > (select curDate from curDT) then (select curDate from curDT)
						else coalesce(EvnSection.EvnSection_disDate, (select curDate from curDT)) end
					) 
				as "EvnSecdni",
				coalesce(
					case when MP.Person_SurName is not null then upper(left(MP.Person_SurName, 1)) || lower(overlay(MP.Person_SurName placing '' from 1 for 1)) || ' ' end ||
					case when MP.Person_FirName is not null then upper(left(MP.Person_FirName, 1)) || '. ' end ||
					case when MP.Person_SecName is not null then upper(left(MP.Person_SecName, 1)) || '.' end
					,''
				) as "MedPersonal_Fio",
				2 as "Status_id",
				case 
					when 
						(
							EvnSection.EvnSection_setDT <= (select curdt from mv)
							and EvnSection.EvnSection_setDT >= (select yesdt from mv)
							or EvnSection.EvnSection_setDate = (select curDate from curDT)
						)
						and (PT.PrehospType_SysNick = 'extreme' or PT.PrehospType_SysNick = 'oper')
						and (EvnSection.EvnSection_disDate > (select curDate from curDT) or EvnSection.EvnSection_disDate is null)
						and (EvnSection.EvnSection_PlanDisDT > (select tomdt from mv) or EvnSection.EvnSection_PlanDisDT is null)
						and coalesce(ERP.EvnReanimatPeriod_id, 0) = 0
					then 'Экстренный'
					when 
						EvnSection.EvnSection_setDate = (select curDate from curDT)						
						and PT.PrehospType_SysNick <> 'extreme' and PT.PrehospType_SysNick <> 'oper'
						and (EvnSection.EvnSection_disDate > (select curDate from curDT) or EvnSection.EvnSection_disDate is null)
						and (EvnSection.EvnSection_PlanDisDT > (select tomdt from mv) or EvnSection.EvnSection_PlanDisDT is null)
						and coalesce(ERP.EvnReanimatPeriod_id, 0) = 0
					then 'Вновь поступивший'
					when
						(
							EvnSection.EvnSection_setDate < (select curDate from curDT)
							and EvnSection.EvnSection_setDT >= (select yesdt from mv)
							and (PT.PrehospType_SysNick <> 'extreme' and PT.PrehospType_SysNick <> 'oper') 
							or EvnSection.EvnSection_setDT < (select yesdt from mv)
						)
						and (EvnSection.EvnSection_PlanDisDT > (select tomdt from mv) or EvnSection.EvnSection_PlanDisDT is null)
						and (EvnSection.EvnSection_disDate > (select curDate from curDT) or EvnSection.EvnSection_disDate is null)
						and coalesce(ERP.EvnReanimatPeriod_id, 0) = 0
					then 'В отделении'
					when
						EvnSection.EvnSection_PlanDisDT <= (select tomdt from mv)
						and (EvnSection.EvnSection_disDate > (select curDate from curDT) or EvnSection.EvnSection_disDate is null)
						and coalesce(ERP.EvnReanimatPeriod_id, 0) = 0
					then 'К выписке'
					when 
						EvnSection.EvnSection_disDate = (select curDate from curDT) --Пациенты, выписанные за текущие сутки.
						and EvnSection.EvnSection_disDate is not null
						and coalesce(ERP.EvnReanimatPeriod_id, 0) = 0
					then 'Выписан'
					when 
						coalesce(ERP.EvnReanimatPeriod_id, 0) <> 0
					then 'В реанимации'
				end as "Status",
				0 as "otherSection",
				
			LSW.LpuSectionWard_id as "LpuSectionWard_id",
			coalesce(LSW.LpuSectionWard_Name, '') as "LpuSectionWard_Name",
			LSW.LpuSectionWard_MainPlace as "LpuSectionWard_MainPlace",
			LSW.LpuSectionWard_MainPlace - NotFreeBed.Evn_cnt as "LpuSectionWard_FreeBedCount",
		
				coalesce(Regime.Regime_id::varchar,'') as "Regime_id",
				coalesce(Regime.Regime_Name,'') as "Regime_Name",
				coalesce(ERP.EvnReanimatPeriod_id, 0) as "EvnReanimatPeriod_id",
				case 
					when LSW.LpuSectionWard_Name is not null then 'Палата ' || LSW.LpuSectionWard_Name 
					else 'Без палаты' 
				end as "Grouping",
				coalesce(DynamicStateMarker.DynamicStateMarker_Status, 0) as "DynamicStateMarker_Status",
				coalesce(EvnPSBarcode.EvnPSBarcode_id, 0) as "EvnPSBarcode_id",
				CASE
					WHEN EvnPSBarcode.EvnPSBarcode_Barcode IS NOT NULL THEN 'barCode'
					WHEN EvnPSBarcode.EvnPSBarcode_RFID IS NOT NULL THEN 'rfidCode'
					ELSE ''
				END as "EvnPSBarcode_Type",
				EvnPSBarcode.EvnPSBarcode_Barcode as "EvnPSBarcode_Barcode",
				EvnPSBarcode.EvnPSBarcode_RFID as "EvnPSBarcode_RFID",
				coalesce(BGT.BloodGroupType_Name, '') || ' ' ||
				case
					when RFT.RhFactorType_Code = '1' then '• Rh+'
					when RFT.RhFactorType_Code = '2' then '• Rh-'
					else ''
				end
				as "BloodGroup_Name",
				case 
					when (select tomdt from mv) = EvnSection.EvnSection_PlanDisDT then 1
					when (select curdt from mv) = EvnSection.EvnSection_PlanDisDT then 2
					when ((select curDate from curDT) = EvnSection.EvnSection_disDate and EvnSection.EvnSection_disDate is not null) then 3
					else 0
				end as "Indicator_PlanDisDT",
				EvnPS.EvnPS_RFID as "EvnPS_RFID",
				PP.PersonPregnancy_id as "PersonPregnancy_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_LpuSection LS  on LS.LpuSection_id = EvnSection.LpuSection_id
				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_PrehospType PT on PT.PrehospType_id = EvnPS.PrehospType_id
				left join v_MesOld Mes  on Mes.Mes_id = EvnSection.Mes_id
				left join v_MedStaffFact MP  on MP.MedStaffFact_id = EvnSection.MedStaffFact_id
				left join v_Post P on P.Post_id = MP.Post_id
				left join v_EvnReanimatPeriod ERP  on ERP.EvnReanimatPeriod_pid = EvnSection.EvnSection_id and ERP.EvnReanimatPeriod_disDT is null
				left join v_PersonEncrypHIV PEH  on PEH.Person_id = Person.Person_id
				left join lateral (
					select
						PersonPregnancy_id
					from v_PersonPregnancy
					where Person_id = Person.Person_id
					order by PersonPregnancy_setDT desc
					limit 1
				) PP on true
				left join lateral(
					select 
						BloodGroupType_id,
						RhFactorType_id
					from v_PersonBloodGroup
					where Person_id = Person.Person_id
					order by PersonBloodGroup_setDT desc
					limit 1
				) PBG on true
				left join v_BloodGroupType BGT on BGT.BloodGroupType_id = PBG.BloodGroupType_id
				left join v_RhFactorType RFT on RFT.RhFactorType_id = PBG.RhFactorType_id
				
			left join v_BedState BS on BS.EvnSection_id = EvnSection.EvnSection_id and BS.BedState_begDate <= (select curdt from mv) and BS.BedState_endDate >= (select curdt from mv)
			left join v_BedProperties BP on BP.Bed_id = BS.Bed_id and BP.BedProperties_begDate <= (select curdt from mv) and BP.BedProperties_endDate is null
			left join v_LpuSectionWard LSW on LSW.LpuSectionWard_id = BP.LpuSectionWard_id
		
				left join lateral(
					select
						PRT.PrescriptionRegimeType_id as Regime_id,
						PRT.PrescriptionRegimeType_Name as Regime_Name
					from v_EvnPrescr EP
						inner join v_EvnPrescrRegime Regime on Regime.EvnPrescrRegime_pid = EP.EvnPrescr_id
						left join PrescriptionRegimeType PRT on PRT.PrescriptionRegimeType_id = Regime.PrescriptionRegimeType_id
					where 1=1
						and EP.Lpu_id = EvnSection.Lpu_id
						and EP.PersonEvn_id = EvnSection.PersonEvn_id
						and EP.PrescriptionType_id = 1
						and Regime.PrescriptionStatusType_id != 3
					order by Regime.EvnPrescrRegime_setDT desc
					limit 1
				) Regime on true
				
			left join lateral (
				select count(*) as BedCount
				from v_BedProperties bp
				left join v_Bed b on b.Bed_id = bp.Bed_id and b.Bed_endDate is null
				where bp.BedProperties_endDate is null and bp.LpuSectionWard_id = LSW.LpuSectionWard_id
			) countBedInWard on true
			left join lateral (
				select count(*) as Evn_cnt
				from v_LpuSectionWard LpuSectionWard
				left join v_BedProperties bp on bp.LpuSectionWard_id = LpuSectionWard.LpuSectionWard_id and bp.BedProperties_endDate is null
				left join v_BedState bs on bs.Bed_id = bp.Bed_id
				where LpuSectionWard.LpuSection_id = '99560027549' and LpuSectionWard.LpuSectionWard_id = lsw.LpuSectionWard_id and bs.BedFondState_id <> 4
				and (
					BS.BedState_endDate between (select curdt from mv) and (select curdt from mv)
					or BS.BedState_begDate between (select curdt from mv) and (select curdt from mv)
					or (select curdt from mv) between BS.BedState_begDate and coalesce(BS.BedState_endDate, (select curdt from mv))
					or (select curdt from mv) between BS.BedState_begDate and coalesce(BS.BedState_endDate, (select curdt from mv))
				)
			) NotFreeBed on true
		
				left join lateral (
					select
						DSM.DynamicStateMarker_Status as DynamicStateMarker_Status
					from
						v_DynamicStateMarker DSM
                 	left join 
                 		v_IndicationPerson IP on IP.IndicationPerson_id = DSM.IndicationPerson_id
					where 1=1
						and IP.Person_id = Person.Person_id
        				and DSM.EvnSection_id = EvnSection.EvnSection_id
        				and datediff('hour', DSM.DynamicStateMarker_begDT::timestamp, (select dbo.tzgetdate())::timestamp) < 8
					order by 
						DSM.DynamicStateMarker_begDT DESC
					limit 1
				) DynamicStateMarker on true
				left join lateral (
					SELECT
						EPSBC.EvnPSBarcode_id as EvnPSBarcode_id,
						EPSBC.EvnPSBarcode_Barcode as EvnPSBarcode_Barcode,
						EPSBC.EvnPSBarcode_RFID as EvnPSBarcode_RFID
					FROM v_EvnPSBarcode EPSBC
					where EPSBC.Person_id = Person.Person_id
						and EPSBC.EvnPS_id = EvnPS.EvnPS_id
						and EPSBC.Lpu_id = EvnSection.Lpu_id
						and EPSBC.EvnPSBarcode_begDate is not null
						and EPSBC.EvnPSBarcode_endDate is null
					limit 1
				) as EvnPSBarcode on true
			where
				EvnSection.Lpu_id = '13101032'
				and EvnSection.LpuSection_id = '99560027549'
				and EvnPS.EvnPS_id is not null
				and cast(EvnSection.EvnSection_setDate as date) <= cast((select curDate from curDT) as date)
				 and MP.MedPersonal_id = '70612'
				 and EvnSection.EvnSection_disDate is null
				and coalesce(EvnSection.EvnSection_isPriem, 1) = 1
			union all
				select
					EvnSection.EvnSection_id as "EvnSection_id",
					EvnSection.EvnSection_rid as "EvnSection_rid",
					EvnSection.LpuSection_id as "LpuSection_id",
					LS.LpuSection_Name as "LpuSection_Name",
					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 
							case 
								when (Person.Person_SurName is null and Person.Person_FirName is null and Person.Person_SecName is null) 
								then 'Неизвестный'
								else 
									coalesce(upper(left(Person.Person_SurName, 1)) || lower(overlay(Person.Person_SurName placing '' from 1 for 1)),'') ||' '|| 
									coalesce(upper(left(Person.Person_FirName, 1)) || lower(overlay(Person.Person_FirName placing '' from 1 for 1)),'') ||' '|| 
									coalesce(upper(left(Person.Person_SecName, 1)) || lower(overlay(Person.Person_SecName placing '' from 1 for 1)),'')
							end
					end as "Person_Fio",
					to_char(Person.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
					case 
							when PEH.PersonEncrypHIV_Encryp is not null then null
							when dbo.Age2(Person.Person_Birthday,(select curDate from curDT)) > 0  
								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",
					null as "InEvnSection_date",
					EvnSection.Person_id as "Person_id",
					EvnSection.Server_id as "Server_id",
					EvnSection.PersonEvn_id as "PersonEvn_id",
					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",
					MP.Person_Fin as "MedPersonal_Fin",
					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",
					EvnSection.MedPersonal_id as "MedPersonal_id",
					datediff(
						'day', 
						EvnSection.EvnSection_setDate, 
						case 
							when EvnSection.EvnSection_disDate > (select curDate from curDT) then (select curDate from curDT) 
							else coalesce(EvnSection.EvnSection_disDate, (select curDate from curDT)) end
						) 
					as "EvnSecdni",
					coalesce(
						case when MP.Person_SurName is not null then upper(left(MP.Person_SurName, 1)) || lower(overlay(MP.Person_SurName placing '' from 1 for 1)) || ' ' end ||
						case when MP.Person_FirName is not null then upper(left(MP.Person_FirName, 1)) || '. ' end ||
						case when MP.Person_SecName is not null then upper(left(MP.Person_SecName, 1)) || '.' end
						,'Без врача'
					)  as "MedPersonal_Fio",
					1 as "Status_id",
					'Переведены из других отделений' as "Status",
					1 as "otherSection",
					
			null as "LpuSectionWard_id",
			'' as "LpuSectionWard_Name",
			null as "LpuSectionWard_MainPlace",
			null as "LpuSectionWard_FreeBedCount",
		
					coalesce(Regime.Regime_id::varchar,'') as "Regime_id",
					coalesce(Regime.Regime_Name,'Без режима') as "Regime_Name",
					coalesce(ERP.EvnReanimatPeriod_id, 0) as "EvnReanimatPeriod_id",
					' Переведены из других отделений' as "Grouping",
					coalesce(DynamicStateMarker.DynamicStateMarker_Status, 0) as "DynamicStateMarker_Status",
					coalesce(EvnPSBarcode.EvnPSBarcode_id, 0) as "EvnPSBarcode_id",
					CASE
						WHEN EvnPSBarcode.EvnPSBarcode_Barcode IS NOT NULL THEN 'barCode'
						WHEN EvnPSBarcode.EvnPSBarcode_RFID IS NOT NULL THEN 'rfidCode'
						ELSE ''
					END as "EvnPSBarcode_Type",
					EvnPSBarcode.EvnPSBarcode_Barcode as "EvnPSBarcode_Barcode",
					EvnPSBarcode.EvnPSBarcode_RFID as "EvnPSBarcode_RFID",
					coalesce(BGT.BloodGroupType_Name, '') || ' ' ||
					case
						when RFT.RhFactorType_Code = '1' then '• Rh+'
						when RFT.RhFactorType_Code = '2' then '• Rh-'
						else ''
					end
					as "BloodGroup_Name",
					0 as "Indicator_PlanDisDT",
					EvnPS.EvnPS_RFID as "EvnPS_RFID",
					PP.PersonPregnancy_id as "PersonPregnancy_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_LpuSection LS  on LS.LpuSection_id = EvnSection.LpuSection_id
					left join Diag Diag  on Diag.Diag_id = EvnSection.Diag_id
					left join v_EvnPS EvnPS  on EvnPS.EvnPS_id = EvnSection.EvnSection_pid
					left join v_PrehospType PT on PT.PrehospType_id = EvnPS.PrehospType_id
					left join v_MesOld Mes  on Mes.Mes_id = EvnSection.Mes_id
					left join v_MedStaffFact MP  on MP.MedStaffFact_id = EvnSection.MedStaffFact_id
					left join v_Post P on P.Post_id = MP.Post_id
					left join v_EvnReanimatPeriod ERP  on ERP.EvnReanimatPeriod_pid = EvnSection.EvnSection_id and ERP.EvnReanimatPeriod_disDT is null
					left join v_PersonEncrypHIV PEH  on PEH.Person_id = Person.Person_id
					left join lateral (
						select
							PersonPregnancy_id
						from v_PersonPregnancy
						where Person_id = Person.Person_id
						order by PersonPregnancy_setDT desc
						limit 1
					) PP on true
					left join lateral(
						select 
							BloodGroupType_id,
							RhFactorType_id
						from v_PersonBloodGroup
						where Person_id = Person.Person_id
						order by PersonBloodGroup_setDT desc
						limit 1
					) PBG on true
					left join v_BloodGroupType BGT on BGT.BloodGroupType_id = PBG.BloodGroupType_id
					left join v_RhFactorType RFT on RFT.RhFactorType_id = PBG.RhFactorType_id
					
			left join v_BedState BS on BS.EvnSection_id = EvnSection.EvnSection_id and BS.BedState_begDate <= (select curdt from mv) and BS.BedState_endDate >= (select curdt from mv)
			left join v_BedProperties BP on BP.Bed_id = BS.Bed_id and BP.BedProperties_begDate <= (select curdt from mv) and BP.BedProperties_endDate is null
			left join v_LpuSectionWard LSW on LSW.LpuSectionWard_id = BP.LpuSectionWard_id
		
					left join lateral(
						select
							PRT.PrescriptionRegimeType_id as Regime_id,
							PRT.PrescriptionRegimeType_Name as Regime_Name
						from v_EvnPrescr EP
							inner join v_EvnPrescrRegime Regime on Regime.EvnPrescrRegime_pid = EP.EvnPrescr_id
							left join PrescriptionRegimeType PRT on PRT.PrescriptionRegimeType_id = Regime.PrescriptionRegimeType_id
						where 1=1
							and EP.Lpu_id = EvnSection.Lpu_id
							and EP.PersonEvn_id = EvnSection.PersonEvn_id
							and EP.PrescriptionType_id = 1
							and Regime.PrescriptionStatusType_id != 3
						order by Regime.EvnPrescrRegime_setDT desc
						limit 1
					) Regime on true
					
                    left join lateral (
						select
							DSM.DynamicStateMarker_Status as DynamicStateMarker_Status
						from
							v_DynamicStateMarker DSM
						left join 
							v_IndicationPerson IP on IP.IndicationPerson_id = DSM.IndicationPerson_id
						where 1=1
							and IP.Person_id = Person.Person_id
							and DSM.EvnSection_id = EvnSection.EvnSection_id
							and datediff('hour', DSM.DynamicStateMarker_begDT::timestamp, (select dbo.tzgetdate())::timestamp) < 8
						order by 
							DSM.DynamicStateMarker_begDT DESC
						limit 1
					) DynamicStateMarker on true
					left join lateral (
						SELECT
							EPSBC.EvnPSBarcode_id as EvnPSBarcode_id,
							EPSBC.EvnPSBarcode_Barcode as EvnPSBarcode_Barcode,
							EPSBC.EvnPSBarcode_RFID as EvnPSBarcode_RFID
						FROM v_EvnPSBarcode EPSBC
						where EPSBC.Person_id = Person.Person_id
							and EPSBC.EvnPS_id = EvnPS.EvnPS_id
							and EPSBC.Lpu_id = EvnSection.Lpu_id
							and EPSBC.EvnPSBarcode_begDate is not null
							and EPSBC.EvnPSBarcode_endDate is null
						limit 1
					) as EvnPSBarcode on true
				where
					EvnSection.EvnSection_id in (select EvnSection_id from other )
					and not exists (
						select 
							t.EvnSection_id
						from v_EvnSection t 
						where 
							t.EvnSection_pid = EvnSection.EvnSection_pid
							and t.EvnSection_setDT >= EvnSection.EvnSection_disDT
							and t.EvnSection_id <> EvnSection.EvnSection_id
						limit 1
					)
					 and MP.MedPersonal_id = '70612'
					
					
				union all
				select
					EvnDirection.EvnDirection_id as "EvnSection_id",
					EvnDirection.EvnDirection_rid as "EvnSection_rid",
					EvnDirection.LpuSection_id as "LpuSection_id",
					LS.LpuSection_Name as "LpuSection_Name",
					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 
							case 
								when (Person.Person_SurName is null and Person.Person_FirName is null and Person.Person_SecName is null) 
								then 'Неизвестный'
								else 
									coalesce(upper(left(Person.Person_SurName, 1)) || lower(overlay(Person.Person_SurName placing '' from 1 for 1)),'') ||' '|| 
									coalesce(upper(left(Person.Person_FirName, 1)) || lower(overlay(Person.Person_FirName placing '' from 1 for 1)),'') ||' '|| 
									coalesce(upper(left(Person.Person_SecName, 1)) || lower(overlay(Person.Person_SecName placing '' from 1 for 1)),'')
							end
					end as "Person_Fio",
					to_char(Person.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
					case 
							when PEH.PersonEncrypHIV_Encryp is not null then null
							when dbo.Age2(Person.Person_Birthday,(select curDate from curDT)) > 0  
								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(EvnDirection.EvnDirection_setDate, 'dd.mm.yyyy'), '') as "EvnSection_setDate",
					coalesce(to_char(EvnDirection.EvnDirection_disDate, 'dd.mm.yyyy'), '') as "EvnSection_disDate",
					null as "InEvnSection_date",
					EvnDirection.Person_id as "Person_id",
					EvnDirection.Server_id as "Server_id",
					EvnDirection.PersonEvn_id as "PersonEvn_id",
					case 
						when exists(
							select *
							from v_PersonQuarantine PQ 
							where 
								PQ.Person_id = EvnDirection.Person_id 
								and PQ.PersonQuarantine_endDT is null
						) then 2 else 1 
					end as "PersonQuarantine_IsOn",
					MP.Person_Fin as "MedPersonal_Fin",
					case 
						when exists(
							select *
							from v_EvnSection ES 
							inner join v_DiagFinance DF  on DF.Diag_id = ES.Diag_id
							where 
								ES.EvnSection_pid = EvnDirection.EvnDirection_pid
								and DF.DiagFinance_IsRankin = 2
						) then 2 else 1 
					end as "DiagFinance_IsRankin",
					null as "EvnSection_PlanDisDT",
					Diag.Diag_Code as "Diag_Code",
					Diag.Diag_Name as "Diag_Name",
					null as "EvnPS_NumCard",
					null as "Mes_id",
					null as "Mes_Code",
					null as "KoikoDni",
					EvnPS.EvnPS_id as "EvnPS_id",
					EvnDirection.MedPersonal_id as "MedPersonal_id",
					datediff(
						'day', 
						EvnDirection.EvnDirection_setDate, 
						case 
							when EvnDirection.EvnDirection_disDate > (select curDate from curDT) then (select curDate from curDT)
							else coalesce(EvnDirection.EvnDirection_disDate, (select curDate from curDT)) end
						) 
					as "EvnSecdni",
					coalesce(
						case when MP.Person_SurName is not null then upper(left(MP.Person_SurName, 1)) || lower(overlay(MP.Person_SurName placing '' from 1 for 1)) || ' ' end ||
						case when MP.Person_FirName is not null then upper(left(MP.Person_FirName, 1)) || '. ' end ||
						case when MP.Person_SecName is not null then upper(left(MP.Person_SecName, 1)) || '.' end
						,'Без врача'
					)  as "MedPersonal_Fio",
					0 as "Status_id",
					'Не поступал' as "Status",
					0 as "otherSection",
					
			null as "LpuSectionWard_id",
			'' as "LpuSectionWard_Name",
			null as "LpuSectionWard_MainPlace",
			null as "LpuSectionWard_FreeBedCount",
		
					'' as "Regime_id",
					'' as Regime_Name,
					coalesce(ERP.EvnReanimatPeriod_id, 0) as "EvnReanimatPeriod_id",
					'Не поступал' as "Grouping",
					coalesce(DynamicStateMarker.DynamicStateMarker_Status, 0) as "DynamicStateMarker_Status",
					coalesce(EvnPSBarcode.EvnPSBarcode_id, 0) as "EvnPSBarcode_id",
					CASE
						WHEN EvnPSBarcode.EvnPSBarcode_Barcode IS NOT NULL THEN 'barCode'
						WHEN EvnPSBarcode.EvnPSBarcode_RFID IS NOT NULL THEN 'rfidCode'
						ELSE ''
					END as "EvnPSBarcode_Type",
					EvnPSBarcode.EvnPSBarcode_Barcode as "EvnPSBarcode_Barcode",
					EvnPSBarcode.EvnPSBarcode_RFID as "EvnPSBarcode_RFID",
					coalesce(BGT.BloodGroupType_Name, '') || ' ' ||
					case
						when RFT.RhFactorType_Code = '1' then '• Rh+'
						when RFT.RhFactorType_Code = '2' then '• Rh-'
						else ''
					end 
					as "BloodGroup_Name",
					0 as "Indicator_PlanDisDT",
					EvnPS.EvnPS_RFID as "EvnPS_RFID",
					PP.PersonPregnancy_id as "PersonPregnancy_id"
				from
					v_EvnDirection EvnDirection 
					left join lateral (
						select * 
						from v_Person_all Person  
						where 
							Person.Server_id = EvnDirection.Server_id
							and Person.Person_id = EvnDirection.Person_id
							and Person.PersonEvn_id = EvnDirection.PersonEvn_id
						limit 1
					) Person on true
					inner join Evn Evn on Evn.Evn_id = EvnDirection.EvnDirection_id and Evn.Evn_deleted = 1
					left join v_LpuSection LS  on LS.LpuSection_id = EvnDirection.LpuSection_id
					left join Diag Diag  on Diag.Diag_id = EvnDirection.Diag_id
					left join v_EvnPS EvnPS  on EvnPS.EvnPS_id = EvnDirection.EvnDirection_pid				
					left join v_PrehospType PT on PT.PrehospType_id = EvnPS.PrehospType_id
					left join v_MedStaffFact MP  on MP.MedStaffFact_id = EvnDirection.MedStaffFact_id
					left join v_Post P on P.Post_id = MP.Post_id
					left join v_EvnReanimatPeriod ERP  on ERP.EvnReanimatPeriod_pid = EvnDirection.EvnDirection_id and ERP.EvnReanimatPeriod_disDT is null
					left join v_PersonEncrypHIV PEH  on PEH.Person_id = Person.Person_id
					left join lateral (
						select
							PersonPregnancy_id
						from v_PersonPregnancy
						where Person_id = Person.Person_id
						order by PersonPregnancy_setDT desc
						limit 1
					) PP on true
					left join lateral(
						select 
							BloodGroupType_id,
							RhFactorType_id
						from v_PersonBloodGroup
						where Person_id = Person.Person_id
						order by PersonBloodGroup_setDT desc
						limit 1
					) PBG on true
					left join v_BloodGroupType BGT on BGT.BloodGroupType_id = PBG.BloodGroupType_id
					left join v_RhFactorType RFT on RFT.RhFactorType_id = PBG.RhFactorType_id
					left join lateral (
						select
							DSM.DynamicStateMarker_Status as DynamicStateMarker_Status
						from
							v_DynamicStateMarker DSM
						left join 
							v_IndicationPerson IP on IP.IndicationPerson_id = DSM.IndicationPerson_id
						where
							IP.Person_id = Person.Person_id
							and DSM.EvnSection_id = EvnDirection.EvnDirection_pid
							and datediff('hour', DSM.DynamicStateMarker_begDT::timestamp, (select dbo.tzgetdate())::timestamp) < 8
						order by 
							DSM.DynamicStateMarker_begDT DESC
						limit 1
					) DynamicStateMarker on true
					left join lateral (
						SELECT
							EPSBC.EvnPSBarcode_id as EvnPSBarcode_id,
							EPSBC.EvnPSBarcode_Barcode as EvnPSBarcode_Barcode,
							EPSBC.EvnPSBarcode_RFID as EvnPSBarcode_RFID
						FROM v_EvnPSBarcode EPSBC
						where EPSBC.Person_id = Person.Person_id
							and EPSBC.EvnPS_id = EvnPS.EvnPS_id
							and EPSBC.Lpu_id = '13101032'
							and EPSBC.EvnPSBarcode_begDate is not null
							and EPSBC.EvnPSBarcode_endDate is null
						limit 1
					) as EvnPSBarcode on true
				where
					EvnDirection.Lpu_did = '13101032'
					and EvnDirection.LpuSection_did = '99560027549'
					and Evn.EvnStatus_id not in (12,13)
					and EvnDirection.EvnDirection_failDT is null
					and cast(Evn.Evn_setDT as date) <= (select curDate from curDT)
					and cast(Evn.Evn_setDT as date) >= (select curDate from curDT)
					 and 1 = 2
					 and 1 = 2
		
