通过循环遍历子查询中的值来执行查询

Execute query by looping through values within a subquery

我不知道我想要实现的目标是什么,所以我正在联系这里的专家以提供一些指导。

我创建了一个 excel 宏,它在多个 Oracle 数据库中运行多个查询。我遇到的一个问题是,其中一个查询在执行期间只能在 where 子句中使用单个值,因为我只需要检索第一行数据(其中 rownum=1),并且可能有数百个这样的唯一值。

我能够执行此操作的唯一方法是在 excel 中创建一系列值,打开数据库连接,执行查询,将结果写回电子表格,关闭连接并然后移动到范围内的下一个值,对范围内的每个值重复此操作。这已被证明是极其低效的,所以我正在寻找一种直接在数据库中循环遍历值的方法。

整个查询超过 650 行,但我提供了一个片段以帮助阐明我希望实现的目标。 “START”是子查询的开头,它提取我需要循环的唯一值 (GS_GRP_SEQ),“END”是子查询的结尾。

非常感谢您抽出时间来协助我!

    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

在您的内部 SQL 语句中,添加一个新列,如下所示:

row_number () over (
           partition by <list of columns in each group>
           order by <required columns> desc
         ) rn

然后在外 SQL 过滤器中:

where rn=1

使用@NickW 提供的解决方案,我通过修改代码如下获得了预期的结果:

    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,
row_number () over (
           PARTITION BY GS_GRP_SEQ
           ORDER BY GS_GRP_SEQ, CA.CS_ADR_PRC_DTE desc, RT_TOT_RATE_EFF_DT desc, RT_TOT_RATE_TERM_DT ASC
         ) rn
    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    
   (SELECT DISTINCT GS_GRP_SEQ FROM MNDP.GRPR where CS.CS_NBR = ('123456') and GS_STAT_CD in ('0','2','6') 
   ORDER BY GS_GRP_SEQ 
) where rn=1