
			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 ttal.TimetableTypeAttributeLink_id is not null and  ttg.TimeTableType_id in (1,11,13,3)  
					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 ttlock.TimetableGraf_id = ttg.TimetableGraf_id
						)
			) ttg on true
			where mpd.MedStaffFact_id  IN (50101000094760,50101000042375,50101000067140,50101000096374,50101000095741,50101000027452,50101000017363,50101000011441,50101000011444,50101000017362,50101000023184,50101000087633,50101000091639,50101000017360,50101000017358,50101000011447,50101000011440,50101000014039,50101000013981,50101000013670,50101000059319,50101000094332,50101000023186,50101000020774,50101000008813,50101000009434,50101000059318,50101000011443,50101000011442,50101000023185,50101000096407,50101000013850,50101000020491,50101000023198,50101000041188,50101000041173,50101000042681,50101000041183,50101000064211,50101000043872,50101000058879,50101000058379,50101000058618,50101000041936,50101000042233,50101000042231,50101000041190,50101000096800,50101000041187,50101000041191,50101000041182,50101000041185,50101000058375)
                and mpd.Day_id >= 44686-1
				and mpd.Day_id <= 44699
			 group by mpd.MedStaffFact_id
		
