
				
				
			with curDT as (select dbo.tzGetDate() as curDate)
			
				SELECT 
				case when EvnPS.PrehospStatus_id = 4 then to_char(EvnPS.EvnPS_setDate, 'DD.MM.YYYY') else null end as "EvnPS_setDate",
				coalesce(LT.LeaveType_Name,PWRC.PrehospWaifRefuseCause_Name) as "LeaveType_Name",
				RTRIM(MedPersLSP.LpuSectionProfile_Name) as "MedPersonalProfile_Name",
				Evn.EvnStatus_id as "EvnStatus_id",
				ED.Evn_id as "EvnDirection_id",
				ED.PayType_id as "PayType_id",
				Lpu.Lpu_id as "Lpu_id",
				case when ED.EvnDirection_IsConfirmed = 2 then 'true' else 'false' end as "IsConfirmed",
				Lpu.Org_id as "Org_id",
				Lpu.Org_Nick as "Org_Nick",
				Lpu.Org_Name as "Org_Name",
				RTRIM(Lpu.Lpu_Nick) as "Lpu_Name",
				ED.EvnDirection_Num as "EvnDirection_Num",
				ED.DirType_id as "DirType_id",
				DT.DirType_Code as "DirType_Code",
				DFT.DirFailType_id as "DirFailType_id",
				DFT.DirFailType_Name as "DirFailType_Name",
				RTRIM(DT.DirType_Name) as "DirType_Name",
				ED.LpuSectionProfile_id as "LpuSectionProfile_id",
				RTRIM(LSP.LpuSectionProfile_Name) as "LpuSectionProfile_Name",
				RTRIM(LS.LpuSection_Name) as "LpuSection_Name",
				
			to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday",
			case
				when (PS.Person_SurName is null and PS.Person_FirName is null and PS.Person_SecName is null)
				then 'Неизвестный'
				else
					coalesce(upper(left(PS.Person_SurName, 1)) || lower(overlay(PS.Person_SurName placing '' from 1 for 1)),'') ||' '||
					coalesce(upper(left(PS.Person_FirName, 1)) || lower(overlay(PS.Person_FirName placing '' from 1 for 1)),'') ||' '||
					coalesce(upper(left(PS.Person_SecName, 1)) || lower(overlay(PS.Person_SecName placing '' from 1 for 1)),'')
			end as "Person_Fio",
			coalesce(upper(left(PS.Person_SurName, 1)) || lower(overlay(PS.Person_SurName placing '' from 1 for 1)),'') as "Person_SurName",
			coalesce(upper(left(PS.Person_FirName, 1)) || lower(overlay(PS.Person_FirName placing '' from 1 for 1)),'') as "Person_FirName",
			coalesce(upper(left(PS.Person_SecName, 1)) || lower(overlay(PS.Person_SecName placing '' from 1 for 1)),'') as "Person_SecName",
			
            case 
                
                when datediff('day', PS.Person_Birthday, (select curDate from curDT)) <= 31
                    then '' || datediff('day', PS.Person_Birthday, (select curDate from curDT)) ||
                    case
                        when 
                            datediff('day', PS.Person_Birthday, (select curDate from curDT)) >= 2
                            and datediff('day', PS.Person_Birthday, (select curDate from curDT)) <= 4
                            or
                            datediff('day', PS.Person_Birthday, (select curDate from curDT)) >= 22
                            and datediff('day', PS.Person_Birthday, (select curDate from curDT)) + 1 <= 24
                        then ' дня'
                        when datediff('day', PS.Person_Birthday, (select curDate from curDT)) in (1, 21, 31)
                        then ' день'
                        else ' дней'
                    end
                when datediff('day', PS.Person_Birthday, (select curDate from curDT)) + 1 > 31 and dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT)) < 12
                    then '' || dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT))::varchar || ' мес.'
                    
                when dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT)) >= 12 and dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT)) < 36
                    then '' || dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar || 
                        case
                            when dbo.Age2(PS.Person_Birthday, (select curDate from curDT)) = 2 then ' года '
                            else ' год '
                        end ||
                        case
                            when (dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT)) - (dbo.Age2(PS.Person_Birthday, (select curDate from curDT)) * 12)) < 1 then ''
                            else (dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT)) - (dbo.Age2(PS.Person_Birthday, (select curDate from curDT)) * 12))::varchar || ' мес.'
                        end
                        
                when dbo.Age2(PS.Person_Birthday, (select curDate from curDT)) >= 3  
                    then '' || dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar || 
                        case
                    when 
                        right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,2)::integer >= 11 
                        and right(dbo.Age2(PS.Person_Birthday,(select curDate from curDT))::varchar,2)::integer <= 14 
                        or right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,1)::integer >= 5 
                        and right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,1)::integer <= 9 
                        or right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,1)::integer = 0 
                    then ' лет' 
                    when 
                        right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,1)::integer >= 2  
                        and right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,1)::integer <= 4 
                    then ' года' 
                    when right(dbo.Age2(PS.Person_Birthday, (select curDate from curDT))::varchar,1)::integer = 1 
                    then ' год' 
                end
                    else '' || dbo.Age_newborn(PS.Person_Birthday, (select curDate from curDT))::varchar || ' мес.' 
            end 
            as "Person_Age"
        ,
			PS.Sex_id as "Sex_id",
		
				ED.Diag_id as "Diag_id",
				coalesce(Diag.Diag_Code,'') as "Diag_Code",
				coalesce(Diag.Diag_Name,'') as "Diag_sName",
				RTRIM(Diag.Diag_FullName) as "Diag_Name",
				ED.EvnDirection_Descr as "EvnDirection_Descr",
				to_char(ED.EvnDirection_desDT, 'DD.MM.YYYY') as "EvnDirection_desDT",
				ED.MedPersonal_id as "MedPersonal_id",
				ED.LpuSection_id as "LpuSection_id",
				MP.Person_Fin as "MedPersonal_Fio",
				coalesce(upper(left(MP.Person_SurName, 1)) || lower(overlay(MP.Person_SurName placing '' from 1 for 1)),'') as "MedPersonal_SurName",
				coalesce(upper(left(MP.Person_FirName, 1)) || lower(overlay(MP.Person_FirName placing '' from 1 for 1)),'') as "MedPersonal_FirName",
				coalesce(upper(left(MP.Person_SecName, 1)) || lower(overlay(MP.Person_SecName placing '' from 1 for 1)),'') as "MedPersonal_SecName",
				Evn.Person_id as "Person_id",
				Evn.PersonEvn_id as "PersonEvn_id",
				Evn.Server_id as "Server_id",
				to_char(Evn.Evn_setDT, 'DD.MM.YYYY') as "EvnDirection_setDate",
				left(cast(Evn.Evn_setDT as time)||'',5) as "EvnDirection_setTime",
				coalesce(to_char(TTS.TimetableStac_setDate, 'DD.MM.YYYY'),'Очередь') as "TimetableStac_setDate",
				coalesce(to_char(TTS.TimetableStac_setDate, 'DD.MM.YYYY HH24:MI'),'Очередь') as "TimetableStac_setDT",
				case
					when
						TTS.TimetableStac_setDate is not null
					then
						null
					else
						datediff('day', EQ.EvnQueue_setDT, (select curDate from curDT))
				end as "EvnQueue_Days",
				EQ.EvnQueue_id as "EvnQueue_id",
				TTS.TimetableStac_id as "TimetableStac_id",
				LSBP.LpuSectionBedProfile_id as "LpuSectionBedProfile_id",
				LSBP.LpuSectionBedProfile_Name as "LpuSectionBedProfile_Name",
				ED.PrehospDirect_id as "PrehospDirect_id",
				ED.Lpu_sid as "Lpu_did",
				ED.LpuSection_id as "LpuSection_did",
				ED.MedStaffFact_id as "MedStaffFact_did",
				to_char(Evn.Evn_setDT, 'dd.mm.yyyy hh24:mi') as "EvnDirection_setDT"
				
				,phs.PrehospStatus_id, phs.PrehospStatus_Name
				
				FROM  
				
				EvnDirection ED
				inner join Evn Evn on Evn.Evn_id = ED.Evn_id and Evn.Evn_deleted = 1
				inner join v_Lpu Lpu on Lpu.Lpu_id = COALESCE(ED.Lpu_sid, Evn.Lpu_id)
				inner join v_DirType DT on DT.DirType_id = ED.DirType_id
				inner join v_PersonState PS on Evn.Person_id = PS.Person_id 
				LEFT JOIN LATERAL (
					select
						EQ.EvnQueue_id,
						EQ.EvnQueue_setDT
					from
						v_EvnQueue EQ
					where EQ.EvnDirection_id = ED.Evn_id
						and EQ.EvnQueue_recDT is null
						and EQ.EvnQueue_failDT is null
						and EQ.EvnQueue_IsArchived is null
						limit 1
				) EQ on true
				LEFT JOIN LATERAL (
					select *
					from v_MedPersonal
					where MedPersonal_id = ED.MedPersonal_id
						and Lpu_id = Lpu.Lpu_id
					limit 1
				) MP on true
				left join v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = ED.LpuSectionProfile_id
				left join v_Diag Diag  on Diag.Diag_id = ED.Diag_id
				left join v_LpuSection LS  on ED.LpuSection_did = LS.LpuSection_id
				left join v_MedService MS on MS.MedService_id = ED.MedService_id and MS.Lpu_id = '140101000000008'
				left join v_MedServiceType MST on MST.MedServiceType_id = MS.MedServiceType_id
				left join v_TimetableStac_lite TTS  on TTS.Evn_id = ED.Evn_id
				left join v_EvnPS EvnPS on EvnPS.EvnDirection_id = ED.Evn_id
				left join v_MedStaffFact MSF on MSF.MedStaffFact_id = ED.MedStaffFact_id
				left join v_LpuSectionProfile MedPersLSP on MedPersLSP.LpuSectionProfile_id = MSF.LpuSectionProfile_id
				left join v_LeaveType LT on EvnPS.LeaveType_id = LT.LeaveType_id
				left join v_PrehospWaifRefuseCause PWRC on PWRC.PrehospWaifRefuseCause_id = EvnPS.PrehospWaifRefuseCause_id
				left join v_DirFailType DFT on DFT.DirFailType_id = ED.DirFailType_id
				left join v_LpuSectionBedProfile LSBP on LSBP.LpuSectionBedProfile_id = ED.LpuSectionBedProfile_id
				
				
				LEFT JOIN LATERAL (
					SELECT *
					FROM v_EvnPS
					WHERE EvnDirection_id = ED.Evn_id
					LIMIT 1
				) EPS on true
				LEFT JOIN v_PrehospStatus phs ON EPS.PrehospStatus_id = phs.PrehospStatus_id
			
				 WHERE 
				cast(Evn.Evn_setDT as DATE) <= cast('2022-01-20' as date)
				and cast(Evn.Evn_setDT as DATE) >= cast('2022-01-01' as date)
				and Evn.EvnClass_id in (27, 117, 49, 201, 202, 199, 56, 181, 216)
				 and Evn.EvnStatus_id not in (12,13) and ED.EvnDirection_failDT is null and MedPersLSP.LpuSectionProfile_id = '140101000000025' and coalesce(Diag.Diag_Code, '') not between 'F10.0' and 'F19.9' and coalesce(Diag.Diag_Code, '') not between 'F20.0' and 'F99' and coalesce(Diag.Diag_Code, '') not between 'F00.0' and 'F09' and coalesce(Diag.Diag_Code, '') not between 'A50.0' and 'A63.8' and (ED.Lpu_did = '140101000000008' or ED.Org_oid = '140101000029928')
				
				and DT.DirType_Code in (1,4,5,6)
				
				
						ORDER BY 
							
				Evn.Evn_setDT DESC
				
				OFFSET 0 ROWS		
				FETCH NEXT 100 ROWS ONLY 
				
