
				
				
            
                with t as (
                    select
                        t.Evn_id,
                        t.EvnClass_Name,
                        t.EvnClass_SysNick,
                        t.Evn_rid,
                        t.Person_id,
                        t.PersonEvn_id,
                        t.Server_id,
                        t.Evn_insDT,
                        t.Evn_setDT,
                        t.Evn_disDT,
                        t.Evn_didDT,
                        t.Evn_signDT,					
                        t.Evn_IsArchive
                    from v_Evn t
                    where
                        t.Person_id = '5471689'
                        and t.EvnClass_SysNick in('EvnDoctor','EvnUslugaCommon','EvnUslugaOper','EvnUslugaPar','EvnPS','EvnSection','EvnDie','EvnLeave','EvnOtherLpu','EvnOtherSection','EvnOtherStac','StickFSSData','DistMonitoring','EvnReanimatPeriod')
                        and t.Evn_signDT is not null
                        and not exists (
                            select *
                            from v_EvnUslugaPar
                            where EvnUslugaPar_id = t.Evn_id
                                and EvnLabSample_id is not null
                        )
                )
			
            
				SELECT 
				Evn.Evn_id as "Evn_id",
				Evn.EvnClass_Name as "EvnClass_Name",
				Evn.EvnClass_SysNick as "EvnClass_SysNick",
				Evn.Evn_rid as "Evn_rid",
				rEvn.EvnClass_SysNick as "EvnClass_rSysNick",
				Evn.Person_id as "Person_id",
				Evn.PersonEvn_id as "PersonEvn_id",
				Evn.Server_id as "Server_id",
				Evn.Evn_DT as "Evn_DT",
				Evn.EvnStatus_Name as "EvnStatus_Name",
				Evn.EvnStatus_Nick as "EvnStatus_Nick"
				
			
				FROM  
				
				(
				select
					t.Evn_id,
					t.EvnClass_Name,
					t.EvnClass_SysNick,
					t.Evn_rid,
					t.Person_id,
					t.PersonEvn_id,
					t.Server_id,
					to_char(t.Evn_insDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
					t.Evn_insDT as Evn_orderDT,
					'Создано' as EvnStatus_Name,
					'ins' as EvnStatus_Nick
					
				from t
				where
					t.Evn_insDT is not null
			 union 
				select
					t.Evn_id,
					t.EvnClass_Name,
					t.EvnClass_SysNick,
					t.Evn_rid,
					t.Person_id,
					t.PersonEvn_id,
					t.Server_id,
					to_char(t.Evn_setDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
					t.Evn_setDT as Evn_orderDT,
					case when t.EvnClass_SysNick = 'EvnDoctor' then 'Врач назначен' when t.EvnClass_SysNick = 'EvnPS' then 'Поступление' when t.EvnClass_SysNick = 'EvnSection' then 'Поступление' when t.EvnClass_SysNick = 'EvnDie' then 'Установлено' when t.EvnClass_SysNick = 'EvnReanimatPeriod' then 'Начало' else 'Выполнено' end as EvnStatus_Name,
					'set' as EvnStatus_Nick
					
				from t
				where
					t.Evn_setDT is not null
			 union 
				select
					t.Evn_id,
					t.EvnClass_Name,
					t.EvnClass_SysNick,
					t.Evn_rid,
					t.Person_id,
					t.PersonEvn_id,
					t.Server_id,
					to_char(t.Evn_disDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
					t.Evn_disDT as Evn_orderDT,
					case when t.EvnClass_SysNick = 'EvnPS' then 'Исход' when t.EvnClass_SysNick = 'EvnSection' then 'Выписка' when t.EvnClass_SysNick = 'EvnReanimatPeriod' then 'Конец' else 'Завершено' end as EvnStatus_Name,
					'dis' as EvnStatus_Nick
					
				from t
				where
					t.Evn_disDT is not null
			 union 
				select
					t.Evn_id,
					t.EvnClass_Name,
					t.EvnClass_SysNick,
					t.Evn_rid,
					t.Person_id,
					t.PersonEvn_id,
					t.Server_id,
					to_char(t.Evn_didDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
					t.Evn_didDT as Evn_orderDT,
					'Выполнено' as EvnStatus_Name,
					'did' as EvnStatus_Nick
					
				from t
				where
					t.Evn_didDT is not null
			 union 
				select
					t.Evn_id,
					t.EvnClass_Name,
					t.EvnClass_SysNick,
					t.Evn_rid,
					t.Person_id,
					t.PersonEvn_id,
					t.Server_id,
					to_char(t.Evn_signDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
					t.Evn_signDT as Evn_orderDT,
					'Подписано' as EvnStatus_Name,
					'sign' as EvnStatus_Nick
					
				from t
				where
					t.Evn_signDT is not null
			 union 
					select
						t.EvnDie_id,
						t.EvnClass_Name,
						'EvnDie' as EvnClass_SysNick,
						t.EvnDie_rid as EvnDie_rid,
						t.Person_id,
						t.PersonEvn_id,
						t.Server_id,
						to_char(t.EvnDie_expDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
						t.EvnDie_expDT as Evn_orderDT,
						'Экспертиза' as EvnStatus_Name,
						'exp' as EvnStatus_Nick
						
					from v_EvnDie t
					where
						t.Person_id = '5471689'
						and t.EvnDie_expDT is not null
				 union 
					select
						t.EvnDirection_id,
						t.EvnClass_Name,
						'EvnDirection' as EvnClass_SysNick,
						t.EvnDirection_rid as EvnDirection_rid,
						t.Person_id,
						t.PersonEvn_id,
						t.Server_id,
						to_char(t.EvnDirection_failDT,'dd.mm.yyyy hh24:mi') as Evn_DT,
						t.EvnDirection_failDT as Evn_orderDT,
						'Отменено' as EvnStatus_Name,
						'fail' as EvnStatus_Nick
						
					from v_EvnDirection t
					where
						t.Person_id = '5471689'
						and t.EvnDirection_failDT is not null
				) Evn
				left join v_Evn rEvn on rEvn.Evn_id = Evn.Evn_rid
				left join EvnDirection ED on ED.Evn_id = Evn.Evn_id
			 WHERE 
				(ED.Evn_id is null or ED.EvnDirection_IsAuto <> 2) 
			
				
						ORDER BY 
							
				Evn.Evn_orderDT desc
			
				OFFSET 0 ROWS		
				FETCH NEXT 10 ROWS ONLY 
				
