DROP FUNCTION IF EXISTS fn_getambulancedoctorinspection;
CREATE FUNCTION fn_getambulancedoctorinspection("EntryBaseID" uuid)
  RETURNS uuid AS
$BODY$
 Begin
	RETURN 
	(
		Select v.VisitID
		From akuz.T_VISIT v
		Where v.type_ = 505 
		And Exists 
		(
			Select 1
			From akuz.T_ENTRY_BASE eb
			Where eb.type_ = 595 
			And eb.EntryBaseID = "EntryBaseID"
			And eb.AmbulanceEvent = v.AmbulanceEvent
			And eb.EntryDate = v.VisitDate
			And eb.Resource = v.Post
		)
		Limit 1
	);
END $BODY$
  LANGUAGE plpgsql STABLE
  COST 100;
ALTER FUNCTION fn_getambulancedoctorinspection(uuid)
  OWNER TO postgres;
