
			select
				
        		case 
					when qu.inQueueCount > 0 then null
					else min(ttg.first_free_date)
				end as first_free_date,
				case 
					when qu.inQueueCount > 0 then 0
					else sum(ttg.total)
				end 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 != 999902 
							and ttlock.TimetableGraf_id = ttg.TimetableGraf_id
						)
			) ttg on true
			
        		left join lateral (
					select
						qMSF.inQueueCount as qMSF,
						qLSP.inQueueCount as qLSP,
						case 
							when (qMSF.inQueueCount > 0 or qLSP.inQueueCount > 0)
							then 1
							else 0
						end as inQueueCount
					FROM (select 0 as nothing) as tt
					left join lateral (
						select 
						count(qu.EvnQueue_id) as inQueueCount
						from v_EvnQueue qu 
						inner join v_EvnDirection_all ed  on ed.EvnDirection_id = qu.EvnDirection_id
						where
							ed.MedStaffFact_id = mpd.MedStaffFact_id
							and qu.EvnQueueStatus_id = 1
							and qu.QueueFailCause_id is null
							and qu.RecMethodType_id = 1
						) qMSF on true	
					left join lateral (
						select 
						count(qu.EvnQueue_id) as inQueueCount
						from v_EvnQueue qu 
						inner join v_EvnDirection_all ed  on ed.EvnDirection_id = qu.EvnDirection_id
						left join v_MedStaffFact msf  on msf.MedStaffFact_id = mpd.MedStaffFact_id
						where
							qu.LpuSectionProfile_did = msf.LpuSectionProfile_id
							and ed.MedStaffFact_id is null
							and qu.EvnQueueStatus_id = 1
							and qu.QueueFailCause_id is null
							and qu.RecMethodType_id = 1
							and qu.Lpu_id = msf.Lpu_id 
						) qLSP on true			
				) qu on true
        	
			where mpd.MedStaffFact_id IN (520101000071155,520101000013930)
			    and mpd.Day_id >= 44610-1
				and mpd.Day_id <= 44624
			 group by mpd.MedStaffFact_id, qu.inQueueCount 
		
