SELECT
			Post_Code,
			Post_Name,
			MedPersonalPost_Code,
			null Service_Id,
			0 flg
		FROM (
		 
		SELECT
			MedStaffFact.MedStaffFact_id,
			Post.Post_Code,
			Post.Post_Name,
			mpp.MedPersonalPost_Code
		FROM dbo.v_MedStaffFact MedStaffFact  
		INNER JOIN (SELECT dbo.tzGetDate() curDt) cdt ON curDt <= COALESCE(MedStaffFact.WorkData_endDate, curDt)
		INNER JOIN dbo.LpuUnit LpuUnit0   ON LpuUnit0.LpuUnit_id = MedStaffFact.LpuUnit_id AND LpuUnit0.LpuUnit_id = $1
		INNER JOIN nsi.v_PostLink PostLink   on PostLink.Post_lid = MedStaffFact.Post_id
		INNER JOIN nsi.v_Post Post   on Post.Post_id = PostLink.Post_id
		INNER JOIN persis.Post pp   on pp.id =  MedStaffFact.Post_id
		INNER JOIN persis.FRMPPost frp   on frp.id =  pp.frmpEntry_id
		INNER JOIN nsi.MedPersonalPost mpp   on mpp.MedPersonalPost_id =  frp.Frmr_id
	 
		 
		AND (
			 
				1=1
				 
	 	)
	 	 		
		 
			AND Post.Post_Code ::bigint  NOT IN (11,22,41,42,43,44,45,46,47,48,49,50,51,73,77,79,80,81,82,83,84,85,86,88,89,90,92,93,94,95,96,174,149)
		 	
		 	
		 
			AND Post.Post_Code ::bigint  NOT IN (0)
		 	
		 	
	 
		) s
		GROUP BY Post_Code, Post_Name, MedPersonalPost_Code
		union
		select
			Post_Code,
			Post_Name,
			MedPersonalPost_Code,
			Service_Id,
		    1 flg
		from ( 
		SELECT  
			null Post_Code,
			null Post_Name,
			'109' MedPersonalPost_Code,
			'B04.014.004' Service_Id 
		from dbo.v_LpuUnit LpuUnit  
		inner join dbo.v_MedService ms   on ms.LpuUnit_id = LpuUnit.LpuUnit_id
		inner join (SELECT dbo.tzGetDate() curDate) cdt ON cdt.curDate <= COALESCE(ms.MedService_endDT, cdt.curDate)
		inner join dbo.MedServiceType mstyp   on ms.MedServiceType_id=mstyp.MedServiceType_id and mstyp.MedServiceType_Code = 31
		inner join dbo.UslugaComplexMedService uslms   on ms.MedService_id=uslms.MedService_id
		inner join dbo.UslugaComplex usl   on uslms.UslugaComplex_id=usl.UslugaComplex_id and usl.UslugaComplex_Code='B04.014.004.007'
		where LpuUnit.LpuUnit_id = $2
		 LIMIT 1 
	 ) q
		union 
		select distinct 
			null Post_Code, 
			null Post_Name,
			MedPersonalPost_Code,
			Service_Id,
			2 flg
		from( 
		SELECT 
			'109' MedPersonalPost_Code,
			'B04.047.002' Service_Id,
			ms.LpuSection_id,
			uslms.UslugaComplexMedService_id,
			LpuUnit.LpuUnit_FRMOUnitID,
			LpuUnit.LpuUnit_id,
			LpuUnit.FRMOUnit_id
		from dbo.v_LpuUnit LpuUnit  
		inner join dbo.v_MedService ms   on ms.LpuUnit_id = LpuUnit.LpuUnit_id
		inner join (SELECT dbo.tzGetDate() curDate) cdt ON cdt.curDate <= COALESCE(ms.MedService_endDT, cdt.curDate)
		inner join dbo.MedServiceType mstyp   on ms.MedServiceType_id=mstyp.MedServiceType_id and mstyp.MedServiceType_Code = 59
		inner join dbo.UslugaComplexMedService uslms   on ms.MedService_id=uslms.MedService_id
		inner join dbo.UslugaComplexAttribute uca   on uca.UslugaComplex_id = uslms.UslugaComplex_id 
			and uca.UslugaComplexAttributeType_id = 263 and uca.UslugaComplexAttribute_DBTableID = 31
		inner join dbo.UslugaComplex usl   on uslms.UslugaComplex_id=usl.UslugaComplex_id
	 ) x
		where x.LpuUnit_id = $3
		 and (  (1=1)
					
			
			
			OR 1 = 2 )
