
			select
				min(ttg.first_free_date) as first_free_date,
				sum(ttg.total) as total,
			    mpd.MedStaffFact_id as "MedStaffFact_id",
				sum(mpd.MedpersonalDay_FreeRec) as free,
				sum(mpd.MedPersonalDay_InetRec) as inet,
				sum(mpd.MedpersonalDay_ReservRec) as reserved,
				sum(mpd.MedpersonalDay_PayRec) as pay
			from MedPersonalDay mpd
			
			left join lateral (
				select 
					count(ttg.TimeTableGraf_id) as total,
					min(case when ttg.Person_id is null  and  ttg.TimeTableType_id in (1,11,18,13)  
					and ttg.TimeTableGraf_begTime > DATEADD('minute', 15, dbo.tzGetDate()) 
					then ttg.TimeTableGraf_begTime else null end) as first_free_date
				from v_TimetableGraf_lite ttg 
				left join TimetableTypeAttributeLink ttal  on ttal.TimetableType_id = ttg.TimetableType_id and  ttal.TimetableTypeAttribute_id in (8,9) 
				left join v_EvnQueue queue  on queue.EvnDirection_id = ttg.EvnDirection_id
				left join v_EvnDirection ed  on ed.EvnDirection_id = queue.EvnDirection_id
				left join v_MedStaffFact msf  on msf.MedStaffFact_id = ed.MedStaffFact_id
				where mpd.Day_id = ttg.TimetableGraf_Day 
					and mpd.MedStaffFact_id = ttg.MedStaffFact_id
					and coalesce(queue.EvnQueueStatus_id, 0) != 1
					and ttg.TimetableGraf_id not in(
							select TimetableGraf_id
							from TimetableLock ttlock 
							where (1=1)
							 and pmUser_insID != '450101000003946' 
							and ttlock.TimetableGraf_id = ttg.TimetableGraf_id
						)
			) ttg on true
			where mpd.MedStaffFact_id IN (450101000102806,450101000112848,450101000100556)
			    and mpd.Day_id >= 44691-1
				and mpd.Day_id <= 44721
			 group by mpd.MedStaffFact_id
		
