CREATE OR REPLACE FUNCTION MEDMMPM_SUBGRU_COC(
  P_CDFILIAL         IN VARCHAR2,
  P_CDCIA            IN VARCHAR2,
  P_CDMANCIA         IN VARCHAR2,
  P_NRSERVICOCIAFILI IN NUMBER,
  P_CDCLASSE         IN VARCHAR2,
  P_CDSUBGRU         IN VARCHAR2,
  P_NRSEQSUBGRUSEMAM IN NUMBER,
  P_NRORG            IN NUMBER)
RETURN FLOAT
AS
  V_MEDMMPM FLOAT;
BEGIN
  SELECT AVG(MMPM_SUBGRU_COC(CSM.CDFILIAL, CSM.CDCIA, CSM.CDMANCIA, CSM.NRSERVICOCIAFILI, CSM.CDCLASSE, CSM.CDSUBGRU, CSM.NRSEQSUBGRUSEMAM, CSM.CDCICLO, CSM.NRORG))
  INTO V_MEDMMPM
  FROM CICLOSUBGRUSEMAM CSM
  WHERE CSM.CDFILIAL       = P_CDFILIAL
  AND CSM.CDCIA            = P_CDCIA
  AND CSM.CDMANCIA         = P_CDMANCIA
  AND CSM.NRSERVICOCIAFILI = P_NRSERVICOCIAFILI
  AND CSM.CDCLASSE         = P_CDCLASSE
  AND CSM.CDSUBGRU         = P_CDSUBGRU
  AND CSM.NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
  AND CSM.NRORG            = P_NRORG;
  RETURN NVL(V_MEDMMPM, 0);
END;
/