title: Execute query by looping through values within a subquery
body: <p>I'm at loss of what I'm trying to achieve is even possible, so I'm reaching out to the experts here to provide some guidance.</p>
  <p>I have created an excel macro which runs several queries across multiple Oracle DBs. One problem I have is that one of the queries can only use a single value in the where clause during execution as I need to retrieve only the first row of data (where rownum=1) and there can be several hundred of these unique values.</p>
  <p>The only way I have been able to execute this is to create a range of values in excel, open the DB connection, execute the query, write the results back to the spreadsheet, close the connection and then move onto the next value in the range, repeating this for every value in the range. This has proven to be extremely inefficient so I'm looking for a way to loop through the values directly in the DB.</p>
  <p>The entire query is over 650 lines, but I've provided a snippet to help provide some clarity on what I'm looking to achieve.
  &quot;START&quot; is the beginning of the subquery which pulls the unique values (GS_GRP_SEQ) that I need to loop through and &quot;END&quot; is the end of the subquery.</p>
  <p>I sincerely appreciate your time in assisting me with this!</p>
  <pre><code>    SELECT * FROM 
      (SELECT TRIM(CS.CS_NBR) AS CS_Num, TRIM(CS.CS_NAM) AS CName, 
      TO_CHAR(to_date(CS.CS_EFF_DAT,'yyyymmdd'),'mm/dd/yyyy') AS C_Eff_Dte,
      CASE CS.UR_ID
      WHEN 'ABC' THEN 'JOHNNY PARKER'
      WHEN '999' THEN 'UNDEFINED'
      END AS UR_Name,
      G.GS_GRP_SEQ as GR_Num, 
      Case G.GS_STAT
      WHEN '0' THEN 'READY'
      END AS GR_Stat,
      CASE G.GS_FUN_ARR
      WHEN '1' THEN 'INCREASED'
      WHEN '9' THEN 'DECREASED'
      END AS Type_Desc,
      R.RT_TOT_RATE AS RATE_TOT,
      TO_CHAR(to_date(R.RT_TOT_RATE_EFF_DT,'yyyymmdd'),'mm/dd/yyyy') as RT_Eff_Dte, 
      R.RT_TOT_1 as Rate_Type_1, R.RT_AMT_1 as Rate_Amount_1, 
      R.RT_TOT_2 as Rate_Type_2, R.RT_AMT_2 as Rate_Amount_2
      FROM MNDB.CASE CS 
      inner join MNDB.CADR CA on CA.CS_ROOT_KEY = CS.CS_NBR
      inner join MNDB.GRPR G on CS.CS_NBR = G.GS_CS_NBR
      inner join MNDB.GCNT GC on GC.GS_GRP_SEQ = G.GS_GRP_SEQ
      inner join MNDB.TOTR R on R.ACCESS_LEVEL = GC.GS_GRP_SEQ
      INNER JOIN MNDB.PROD P ON TRIM(P.PROD_GRP) = TRIM(G.GS_PROD_CD) 
      LEFT JOIN MNDB.GNAT NAT ON G.GS_GRP_SEQ = NAT.GS_GRP_SEQ
      WHERE CS.CS_NBR = ('123456') 
      AND G.GS_GRP_SEQ IN 
  --START--    
     (SELECT DISTINCT GS_GRP_SEQ FROM MNDP.GRPR where CS.CS_NBR = ('123456') and GS_STAT_CD in ('0','2','6') 
  --END--
     ORDER BY CA.CS_ADR_PRC_DTE desc, RT_TOT_RATE_EFF_DT desc, RT_TOT_RATE_TERM_DT ASC ) where rownum=1
  </code></pre>