--CREATE OR REPLACE FUNCTION fn_GetLastVisit("VisitID" uuid)
--  RETURNS uuid AS
--$BODY$
--Declare ret UUID;
--Begin
--	select t1.VisitID
--	from akuz.T_DOCTOR_INSPECTION_RE t1
--	inner join
--		akuz.T_VISIT t2 
--		on t1.VisitID = t2.VisitID 
--		and t1.DoctorInspection = "VisitID"
--	into ret
--	order by t2.VisitDate desc
--	Limit 1;
--	if ret is null 
--		then return "VisitID";
--	end if;
--	return ret;
--END $BODY$
--  LANGUAGE plpgsql STABLE
--  COST 100;
--ALTER FUNCTION fn_getlastvisit(uuid)
--  OWNER TO postgres;