
            with curDT as (select dbo.tzGetDate() as curDate)
			SELECT
				ps.Person_id as "Person_id",
				PS.PersonEvn_id as "PersonEvn_id",
				ps.Server_id as "Server_id",
				ps.Server_pid as "Server_pid",
				dbo.Age_newborn(PS.Person_Birthday, dbo.tzGetDate()) as "Person_AgeMonth",
				COALESCE(to_char(PS.Person_BirthDay, 'DD.MM.YYYY'), '') as "Person_Birthday"
				,Document.DocumentType_id as "DocumentType_id"
,COALESCE(PS.Person_Snils, '') as "Person_Snils"
,
            case 
                
                when datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) <= 31
                    then '' || datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) ||
                    case
                        when 
                            datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) >= 2
                            and datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) <= 4
                            or
                            datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) >= 22
                            and datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) + 1 <= 24
                        then ' дня'
                        when datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) in (1, 21, 31)
                        then ' день'
                        else ' дней'
                    end
                when datediff('day', PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) + 1 > 31 and dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) < 12
                    then '' || dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar || ' мес.'
                    
                when dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) >= 12 and dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) < 36
                    then '' || dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar || 
                        case
                            when dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) = 2 then ' года '
                            else ' год '
                        end ||
                        case
                            when (dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) - (dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) * 12)) < 1 then ''
                            else (dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) - (dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) * 12))::varchar || ' мес.'
                        end
                        
                when dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT))) >= 3  
                    then '' || dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar || 
                        case
                    when 
                        right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,2)::integer >= 11 
                        and right(dbo.Age2(PS.Person_Birthday,COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,2)::integer <= 14 
                        or right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,1)::integer >= 5 
                        and right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,1)::integer <= 9 
                        or right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,1)::integer = 0 
                    then ' лет' 
                    when 
                        right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,1)::integer >= 2  
                        and right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,1)::integer <= 4 
                    then ' года' 
                    when right(dbo.Age2(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar,1)::integer = 1 
                    then ' год' 
                end
                    else '' || dbo.Age_newborn(PS.Person_Birthday, COALESCE(Person.Person_deadDT, (select curDate from curDT)))::varchar || ' мес.' 
            end 
            as "personAgeText"
        
,job.Org_id as "JobOrg_id"
,COALESCE(joborg.Org_Name, '') as "Person_Job"
,COALESCE(OmsSprTerr.OmsSprTerr_id, 0) as "OmsSprTerr_id"
,COALESCE(OmsSprTerr.OmsSprTerr_Code, 0) as "OmsSprTerr_Code"
,Sex.Sex_Code as "Sex_Code"
,Sex.Sex_Name as "Sex_Name"
,Sex.Sex_id as "Sex_id"
,Person.Person_IsUnknown as "Person_IsUnknown"
,Person.Person_IsDead as "Person_IsDead"
,Person.PersonCloseCause_id as "PersonCloseCause_id"
,COALESCE(to_char(Person.Person_deadDT, 'DD.MM.YYYY'), '') as "Person_deadDT"
,COALESCE(to_char(Person.Person_closeDT, 'DD.MM.YYYY'), '') as "Person_closeDT"
,Person.Person_IsAnonym as "Person_IsAnonym"
,COALESCE(ps.Person_SurName, '') || COALESCE(' ' || SUBSTRING(ps.Person_FirName, 1, 1) || '.','') || COALESCE(' ' || SUBSTRING(ps.Person_SecName,1,1) || '.','') as "Person_ShortFio"
,COALESCE(RTRIM(PS.Person_Surname), '') as "Person_Surname"
,COALESCE(RTRIM(PS.Person_Firname), '') as "Person_Firname"
,COALESCE(RTRIM(PS.Person_Secname), '') as "Person_Secname"
,null as "PersonEncrypHIV_Encryp"
,CASE WHEN PQ.PersonQuarantine_id is not null THEN 'true' ELSE 'false' END as "PersonQuarantine_IsOn"
,to_char(PQ.PersonQuarantine_begDT, 'DD.MM.YYYY') as "PersonQuarantine_begDT"
,COALESCE(PS.Person_EdNum, '') as "Person_EdNum"
,cast(PS.Person_SurName as varchar(1)) as "SurNameLetter"
,to_char(PS.Person_syncDT, 'YYYY-MM-DD HH24:MI:SS') as "Person_syncDT",
				E.EvnPS_IsActive as "EvnPS_IsActive"
			FROM
				v_PersonState PS 
				left join lateral (
                    select
                    EvnPS_id,
                    EvnPS_IsActive
                    from
                    v_EvnPS
                    where
                    Person_id = PS.Person_id
                    order by EvnPS_setDT desc LIMIT 1) E on true
				left join Document  on Document.Document_id = PS.Document_id
 left join Person  on Person.Person_id = PS.Person_id
 left join v_Job job  on job.Job_id = ps.Job_id
 left join Org joborg  on joborg.Org_id = job.Org_id
 left join Polis  on Polis.Polis_id = PS.Polis_id
 left join OmsSprTerr  on OmsSprTerr.OmsSprTerr_id = Polis.OmsSprTerr_id
 left join Sex  on Sex.Sex_id = PS.Sex_id
 
			left join lateral (
				select PQ.PersonQuarantine_id, PQ.PersonQuarantine_begDT
				from v_PersonQuarantine PQ
				where PQ.Person_id = ps.Person_id 
				and PQ.PersonQuarantine_endDT is null
				limit 1
			) PQ on true
		
			WHERE
				 (1=1) and PS.Person_id = '1046443' 
			 LIMIT 1 
		
