CREATE OR REPLACE FUNCTION CUSTO_SUBGRU_TOTAL_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,
    P_DATACUSTO        IN VARCHAR2,
    P_TIPOCUSTO        IN VARCHAR2)
  RETURN FLOAT
AS
  V_CUSTO FLOAT;
BEGIN
  V_CUSTO := 0;
  FOR CUR_CICLO IN (
    SELECT CDCICLO FROM CICLOSUBGRUSEMAM
    WHERE CDFILIAL = P_CDFILIAL AND CDCIA = P_CDCIA AND CDMANCIA = P_CDMANCIA
      AND NRSERVICOCIAFILI = P_NRSERVICOCIAFILI AND CDCLASSE = P_CDCLASSE
      AND CDSUBGRU = P_CDSUBGRU AND NRSEQSUBGRUSEMAM = P_NRSEQSUBGRUSEMAM
  ) LOOP
    V_CUSTO := V_CUSTO + CUSTO_SUBGRU_COC(
      P_CDFILIAL,
      P_CDCIA,
      P_CDMANCIA,
      P_NRSERVICOCIAFILI,
      P_CDCLASSE,
      P_CDSUBGRU,
      P_NRSEQSUBGRUSEMAM,
      CUR_CICLO.CDCICLO,
      P_NRORG,
      P_DATACUSTO,
      P_TIPOCUSTO
    );
  END LOOP;
  RETURN V_CUSTO;
END;
/