
			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, (select 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 != '520101000049542' 
							and TimetableGraf_id is not null
						)
			) ttg on true
			
			where mpd.MedStaffFact_id  IN (520101000080461,520101000080038,520101000024458,520101000033873,520101000065275,520101000067140,520101000037157,520101000060671,520101000077412,520101000057619,520101000089753,520101000076779,520101000069709,520101000019555,520101000034145,520101000031466,520101000086115,520101000040420,520101000081332,520101000060262,520101000000965,520101000051703,520101000050122,520101000070982,520101000070414,520101000070655,520101000056918,520101000079088,520101000096463,520101000040788,520101000089371,520101000078695,520101000043116,520101000039514,520101000088790,520101000088286,520101000096771,520101000095706,520101000064838,520101000072659,520101000097994,520101000096770,520101000019116,520101000097887,520101000082089,520101000012601,520101000082199,520101000073852,520101000060004,520101000068146,520101000052963,520101000058743,520101000018002,520101000092792,520101000086519,520101000084451,520101000086518,520101000074709,520101000061319,520101000032040,520101000016854,520101000038006,520101000067479,520101000094127,520101000051284,520101000047051,520101000044410,520101000066604,520101000085038,520101000010242,520101000039796,520101000014040,520101000006151,520101000075842,520101000014376,520101000083108,520101000066887,520101000069090,520101000013284,520101000016298,520101000087540,520101000027183,520101000032977,520101000082067,520101000080652,520101000018212,520101000018872,520101000080825,520101000066905,520101000060661,520101000014822,520101000080266,520101000080267,520101000066216,520101000015085,520101000088300,520101000014565,520101000054892,520101000097465,520101000084155,520101000082163,520101000094264,520101000032039,520101000067740,520101000060649,520101000068337,520101000094818,520101000008711,520101000072469,520101000050954,520101000049229,520101000049697,520101000098285,520101000057193,520101000080690,520101000077226,520101000080413,520101000004529,520101000008248,520101000014066,520101000084880,520101000096581,520101000092555,520101000005815,520101000067473,520101000042176,520101000096592,520101000064331,520101000064007,520101000042177,520101000019114,520101000025770,520101000029896,520101000014158,520101000061559,520101000092746,520101000064170,520101000088924,520101000069115,520101000034448,520101000034672,520101000034108,520101000088926,520101000070197,520101000061752,520101000071269,520101000094400,520101000075544,520101000094670,520101000009773,520101000009834,520101000045375,520101000096645,520101000061469,520101000027476,520101000027489,520101000019601,520101000095428,520101000070079,520101000005738,520101000065061,520101000047537,520101000051184,520101000074474,520101000074569,520101000069876,520101000044985,520101000027485,520101000069435,520101000096617,520101000060558,520101000046706,520101000075262,520101000059133,520101000029448,520101000046707,520101000051198,520101000073733,520101000024543,520101000002755,520101000067512,520101000067902,520101000067908,520101000091116,520101000067815,520101000019558,520101000068651,520101000081846,520101000064116,520101000064115,520101000081296,520101000056119,520101000019984,520101000027498,520101000036585,520101000036210,520101000001276,520101000080096,520101000037066,520101000070875,520101000015811,520101000051936,520101000050622,520101000067820,520101000067879,520101000015712,520101000070997,520101000080654) 
				and mpd.Day_id >= 44692-1
				and mpd.Day_id <= 44706
			 group by mpd.MedStaffFact_id
		
