--CREATE OR REPLACE FUNCTION fn_GetDemandDrugRequestList("DrugDemandID" uuid)
--RETURNS uuid AS
--$BODY$
-- Begin
--	RETURN 
--	(
--		SELECT
--		(
--			SELECT drl.BaseDrugRequestItemID
--			FROM AKUZ.T_BASE_DRUG_REQUEST_ITEM AS drl
--			LEFT JOIN AKUZ.T_REQUEST_CATALOG AS rc ON rc.BaseDrugRequestItemID = drl.BaseDrugRequestItemID 
--			WHERE drl.RequestCode = dd.RequestCode 
--			AND drl.AppListBeginDate/*Элемент зс*/ <= bdr.BeginPeriod/*Заявка*/
--			AND drl.AppListEndDate/*Элемент зс*/ >= bdr.EndPeriod/*Заявка*/
--			AND (rc.BaseDrugRequestItemID IS NULL OR dr.RequestType = rc.BenefitType)
--			AND ((rc.SectionCode is null and  dd.SectionCode is null) or rc.SectionCode = dd.SectionCode)
--			limit 1
--		)
--		FROM AKUZ.T_DRUG_DEMAND AS dd
--		inner join AKUZ.T_DRUG_REQUEST AS dr ON dd.DrugRequest = dr.DrugRequestID
--		inner join AKUZ.T_BASE_DRUG_REQUEST AS bdr ON dd.DrugRequest = bdr.DrugRequestID
--		Where dd.DrugDemandID = "DrugDemandID"
--	);
--END $BODY$
--  LANGUAGE plpgsql STABLE
--  COST 100;
--ALTER FUNCTION fn_getdemanddrugrequestlist(uuid)
--  OWNER TO postgres;