
            
				select
					'TimetableGraf' as "ttObject",
					EQI.ElectronicQueueInfo_id as "ElectronicQueueInfo_id",
					EQI.ElectronicQueueInfo_Code as "ElectronicQueueInfo_Code",
					EQI.ElectronicQueueInfo_Name as "ElectronicQueueInfo_Name",
					EQI.ElectronicQueueInfo_Nick as "ElectronicQueueInfo_Nick",
					EQI.ElectronicQueueInfo_Letter as "ElectronicQueueInfo_Letter",
					ttg.TimeTableType_id as "TimeTableType_id",
					msf.MedStaffFact_id :: text as "MedStaffFact_id",
					( left(msf.Person_Firname, 1)||'.'||coalesce(left(msf.Person_Secname,1)||'.', '') ) as "Doctor_FullNameInitials",
					rtrim(msf.Person_Fin) as "Doctor_ShortFullName",
					rtrim(msf.Person_surname) as "Doctor_Surname",
					null as "UslugaComplexMedService_id",
					null as "UslugaComplex_Name",
					null as "UslugaComplex_Nick",
					rtrim(coalesce(p.Person_Surname, ''))||' '||rtrim(coalesce(p.Person_Firname, ''))||' '||rtrim(coalesce(p.Person_Secname, '')) as "Person_FIO",
					rtrim(coalesce(p.Person_Surname, '')) as "Person_Surname",
					rtrim(coalesce(p.Person_Firname, '')) as "Person_Firname",
					rtrim(coalesce(p.Person_Secname, '')) as "Person_Secname",
					ttg.TimeTableGraf_begTime as "TimeTable_begTime",
					ttg.TimeTableGraf_id as "TimeTable_id",
					ed.EvnDirection_TalonCode as "EvnDirection_TalonCode",
					ed.EvnDirection_id as "EvnDirection_id",
					et.ElectronicTalonStatus_id as "ElectronicTalonStatus_id",
					et.ElectronicTalon_id as "ElectronicTalon_id",
					et.ElectronicTalon_Num as "ElectronicTalon_Num",
					ets.ElectronicTalonStatus_Name as "ElectronicTalonStatus_Name",
					ES.ElectronicService_id as "ElectronicService_id",
					p.Person_id as "Person_id",
					null as "EvnLabRequest_LabSampleNums",
					null as "EvnLabRequest_id",
					et.ElectronicTalon_insDT as "ElectronicTalon_insDT",
					null as "EvnLabRequest_UslugaName",
					datediff('ss', et.ElectronicTalon_insDT, (select dbo.tzGetDate())) as "ElectronicTalon_TimeHasPassed",
					datediff('day', et.ElectronicTalon_insDT, (select dbo.tzGetDate())) as "daysago"
					from v_ElectronicQueueInfo EQI
						inner join v_ElectronicService ES on ES.ElectronicQueueInfo_id = EQI.ElectronicQueueInfo_id
						inner join v_MedServiceElectronicQueue mseq on mseq.ElectronicService_id = ES.ElectronicService_id
						inner join v_MedStaffFact msf on msf.MedStaffFact_id = mseq.MedStaffFact_id
						inner join TimeTableGraf ttg on ttg.MedStaffFact_id = msf.MedStaffFact_id
						inner join v_PersonState p on p.Person_id = ttg.Person_id
						inner join v_EvnDirection_all ed on ed.EvnDirection_id = ttg.EvnDirection_id
						left join v_ElectronicTalon et on et.EvnDirection_id = ed.EvnDirection_id
						left join v_ElectronicTalonStatus ets on ets.ElectronicTalonStatus_id = et.ElectronicTalonStatus_id
					where
						(1=1)
						and ttg.TimetableGraf_Day = 44691.000000
						and ttg.Person_id is not null
						and ttg.EvnDirection_id is not null 
                     and EQI.Lpu_id = '13101856' and p.Person_SurName ilike '%' || 'малл' || '%' 
            
            

            UNION ALL

            
            
            select
			distinct
				'TimetableMedService' as "ttObject",
				EQI.ElectronicQueueInfo_id as "ElectronicQueueInfo_id",
				EQI.ElectronicQueueInfo_Code as "ElectronicQueueInfo_Code",
				EQI.ElectronicQueueInfo_Name as "ElectronicQueueInfo_Name",
				EQI.ElectronicQueueInfo_Nick as "ElectronicQueueInfo_Nick",
				EQI.ElectronicQueueInfo_Letter as "ElectronicQueueInfo_Letter" ,
				ttms.TimeTableType_id as "TimeTableType_id",
				null as "MedStaffFact_id",
				null as "Doctor_FullNameInitials",
				null as "Doctor_ShortFullName",
				null as "Doctor_Surname",
				mseq.UslugaComplexMedService_id as "UslugaComplexMedService_id",
				uc.UslugaComplex_Name as "UslugaComplex_Name",
				uc.UslugaComplex_Nick as "UslugaComplex_Nick",
				rtrim(coalesce(p.Person_Surname, ''))||' '||rtrim(coalesce(p.Person_Firname, ''))||' '||rtrim(coalesce(p.Person_Secname, '')) as "Person_FIO",
				rtrim(coalesce(p.Person_Surname, '')) as "Person_Surname",
				rtrim(coalesce(p.Person_Firname, '')) as "Person_Firname",
				rtrim(coalesce(p.Person_Secname, '')) as "Person_Secname",
				ttms.TimeTableMedService_begTime as "TimeTable_begTime",
				ttms.TimeTableMedService_id as "TimeTable_id",
				ed.EvnDirection_TalonCode as "EvnDirection_TalonCode",
				ed.EvnDirection_id as "EvnDirection_id",
				et.ElectronicTalonStatus_id as "ElectronicTalonStatus_id",
				et.ElectronicTalon_id as "ElectronicTalon_id",
				et.ElectronicTalon_Num as "ElectronicTalon_Num",
				ets.ElectronicTalonStatus_Name as "ElectronicTalonStatus_Name",
				ES.ElectronicService_id as "ElectronicService_id",
				p.Person_id as "Person_id",
				lr.EvnLabRequest_LabSampleNums as "EvnLabRequest_LabSampleNums",
				lr.EvnLabRequest_id as "EvnLabRequest_id",
				et.ElectronicTalon_insDT as "ElectronicTalon_insDT",
				lr.EvnLabRequest_UslugaName as "EvnLabRequest_UslugaName",
				datediff('ss', et.ElectronicTalon_insDT, (select dbo.tzGetDate())) as "ElectronicTalon_TimeHasPassed",
				datediff('day', et.ElectronicTalon_insDT, (select dbo.tzGetDate())) as "daysago"
			
			from v_ElectronicQueueInfo EQI
			inner join v_ElectronicService ES on ES.ElectronicQueueInfo_id = EQI.ElectronicQueueInfo_id
			inner join v_MedServiceElectronicQueue mseq on mseq.ElectronicService_id = ES.ElectronicService_id
			inner join v_TimeTableMedService ttms on (ttms.UslugaComplexMedService_id = mseq.UslugaComplexMedService_id or ttms.MedService_id = mseq.MedService_id)
			left join v_UslugaComplexMedService as ucms on ucms.UslugaComplexMedService_id = mseq.UslugaComplexMedService_id
			left join v_UslugaComplex as uc on uc.UslugaComplex_id = ucms.UslugaComplex_id
			inner join v_PersonState p on p.Person_id = ttms.Person_id
			inner join v_EvnDirection_all ed on ed.EvnDirection_id = ttms.EvnDirection_id
			left join v_ElectronicTalon et on et.EvnDirection_id = ed.EvnDirection_id
			left join v_ElectronicTalonStatus ets on ets.ElectronicTalonStatus_id = et.ElectronicTalonStatus_id
			left join v_EvnLabRequest lr on lr.EvnDirection_id = ed.EvnDirection_id
			where
				(1=1)
				and ttms.TimetableMedService_Day = 44691.000000
				and ttms.Person_id is not null
				and ttms.EvnDirection_id is not null
				 and EQI.Lpu_id = '13101856' and p.Person_SurName ilike '%' || 'малл' || '%' 
		

            UNION ALL

            
			SELECT
				'TimetableResource' as "ttObject",
				EQI.ElectronicQueueInfo_id as "ElectronicQueueInfo_id",
				EQI.ElectronicQueueInfo_Code as "ElectronicQueueInfo_Code",
				EQI.ElectronicQueueInfo_Name as "ElectronicQueueInfo_Name",
				EQI.ElectronicQueueInfo_Nick as "ElectronicQueueInfo_Nick",
				EQI.ElectronicQueueInfo_Letter as "ElectronicQueueInfo_Letter" ,
				ttr.TimeTableType_id as "TimeTableType_id",
				null as "MedStaffFact_id",
				null as "Doctor_FullNameInitials",
				null as "Doctor_ShortFullName",
				null as "Doctor_Surname",
				mseq.UslugaComplexMedService_id as "UslugaComplexMedService_id",
				uc.UslugaComplex_Name as "UslugaComplex_Name",
				uc.UslugaComplex_Nick as "UslugaComplex_Nick",
				rtrim(coalesce(p.Person_Surname, ''))||' '||rtrim(coalesce(p.Person_Firname, ''))||' '||rtrim(coalesce(p.Person_Secname, '')) as "Person_FIO",
				rtrim(coalesce(p.Person_Surname, '')) as "Person_Surname",
				rtrim(coalesce(p.Person_Firname, '')) as "Person_Firname",
				rtrim(coalesce(p.Person_Secname, '')) as "Person_Secname",
				ttr.TimeTableResource_begTime as "TimeTable_begTime",
				ttr.TimetableResource_id as "TimeTable_id",
				ed.EvnDirection_TalonCode as "EvnDirection_TalonCode",
				ed.EvnDirection_id as "EvnDirection_id",
				et.ElectronicTalonStatus_id as "ElectronicTalonStatus_id",
				et.ElectronicTalon_id as "ElectronicTalon_id",
				et.ElectronicTalon_Num as "ElectronicTalon_Num",
				ets.ElectronicTalonStatus_Name as "ElectronicTalonStatus_Name",
				ES.ElectronicService_id as "ElectronicService_id",
				p.Person_id as "Person_id",
				lr.EvnLabRequest_LabSampleNums as "EvnLabRequest_LabSampleNums",
				lr.EvnLabRequest_id as "EvnLabRequest_id",
				et.ElectronicTalon_insDT as "ElectronicTalon_insDT",
				lr.EvnLabRequest_UslugaName as "EvnLabRequest_UslugaName",
				datediff('ss', et.ElectronicTalon_insDT, (select dbo.tzGetDate())) as "ElectronicTalon_TimeHasPassed",
				datediff('day', et.ElectronicTalon_insDT, (select dbo.tzGetDate())) as "daysago"
			FROM v_ElectronicQueueInfo EQI
			inner join v_ElectronicService ES on ES.ElectronicQueueInfo_id = EQI.ElectronicQueueInfo_id
			inner join v_MedServiceElectronicQueue mseq on mseq.ElectronicService_id = ES.ElectronicService_id
			inner join v_TimeTableResource ttr on ttr.Resource_id = mseq.Resource_id
			inner join v_PersonState p on p.Person_id = ttr.Person_id
			inner join v_UslugaComplexResource ucr on ucr.Resource_id = mseq.Resource_id
			inner join v_UslugaComplexMedService ucms on ucms.UslugaComplexMedService_id = ucr.UslugaComplexMedService_id
			inner join v_UslugaComplex uc on uc.UslugaComplex_id = ucms.UslugaComplex_id
			inner join v_EvnDirection_all ed on ed.EvnDirection_id = ttr.EvnDirection_id
			left join v_ElectronicTalon et on et.EvnDirection_id = ed.EvnDirection_id
			left join v_ElectronicTalonStatus ets on ets.ElectronicTalonStatus_id = et.ElectronicTalonStatus_id
			left join v_EvnLabRequest lr on lr.EvnDirection_id = ed.EvnDirection_id
			where
				(1=1)
				and ttr.TimeTableResource_Day = 44691.000000
				and ttr.Person_id is not null
				and ttr.EvnDirection_id is not null
				and uc.Lpu_id = '13101856'
				 and EQI.Lpu_id = '13101856' and p.Person_SurName ilike '%' || 'малл' || '%' 
		

             order by "MedStaffFact_id", "ElectronicTalonStatus_id", "TimeTable_id" asc 
		
