title: Pl / SQL Oracle helps to run a Date in Subquery
body: <p>How could I get the date of the Maximum Value, by means of a subquery
  I can't put the Date in the Main query because I would have to add it to the group by it would bring me a lot of data</p>
  <p>Here is the Code:</p>
  <pre><code>  SELECT MAX (A1.VALOR) AS VALOR,
           (SELECT sq1.FECHA
              FROM VARIABLE_VALORES_SMEC sq1
             WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
              MES,                                  -- {&lt;-- Here is the Problem}
           (SELECT CODIGO_AGENTE
              FROM AGENTES
             WHERE ID_AGENTE = A1.ID_AGENTE)
              Agentess,
           (SELECT NOMBRE_AGENTE
              FROM AGENTES
             WHERE ID_AGENTE = A1.ID_AGENTE)
              Nombre_Agente
      FROM VARIABLE_VALORES_SMEC A1
     WHERE     A1.VALOR &lt; '1'
           AND A1.VALOR != '0'
           AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
                                  FROM VARIABLE_VALORES_SMEC C1
                                 WHERE A1.FECHA = C1.FECHA)
           AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
                                  FROM AGENTES B1
                                 WHERE ID_CATEGORIA_AGENTE = 'AC006')
           AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
                                              -1))
                             AND (LAST_DAY (
                                     ADD_MONTHS (
                                        TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
                                        -1))))
           AND A1.ID_VARIABLE LIKE '%_calc_total_pot@%'
  GROUP BY ID_AGENTE
  </code></pre>