Oracle 11g子查询

时间:2018-11-01 14:52:43

标签: sql oracle oracle11g

我在将第一个查询中的主表“ MAINtbl”链接到子查询时遇到问题。我正在尝试采用“ MAINtbl”中给出的结果,并通过某些关键字段将数据连接到另一个表“ WWPMARS.GPS_CLI_PRD_BASE_INFO F”。


select F.CNTRCT_NBR, F.CNTRCT_LI_NBR, F.BASE_PRC, F.PRD_EFCTV_DT from WWPMARS.GPS_CLI_PRD_BASE_INFO F
inner join ( 
SELECT C.CNTRCT_NBR, C.CNTRCT_LI_NBR, MAX(D.PRD_EFCTV_DT)
  FROM WWPMARS.GPS_CNTRCT_LINE_ITM C, WWPMARS.GPS_CNTRCT_LI_PRD D
 WHERE     (    ( (C.CNTRCT_TYPE_CD = 'B') AND (C.CNTRCT_CTG_CD = 'ST'))
            AND (D.TOT_PRC_ADD_ON_PRC <> '0')
            and C.CNTRCT_NBR||C.CNTRCT_LI_NBR ='3VLZ0000')
       AND (D.PRD_EFCTV_DT <= CURRENT_DATE)
       AND C.CNTRCT_LI_NBR = D.CNTRCT_LI_NBR
       AND C.CNTRCT_NBR = D.CNTRCT_NBR 
       group by C.CNTRCT_NBR, C.CNTRCT_LI_NBR) MAINtbl
       on F.CNTRCT_NBR = C.CNTRCT_NBR and F.CNTRCT_LI_NBR = C.CNTRCT_LI_NBR ;

1 个答案:

答案 0 :(得分:0)

别名C仅在子查询内部可见,而在子查询外部不可见。但是,您正在尝试在外部使用它(最后使用)。然后将其更改为MAINtbl.CNTRCT_LI_NBR,如下所示:

select F.CNTRCT_NBR, F.CNTRCT_LI_NBR, F.BASE_PRC, F.PRD_EFCTV_DT 
from WWPMARS.GPS_CLI_PRD_BASE_INFO F
inner join ( 
SELECT C.CNTRCT_NBR, C.CNTRCT_LI_NBR, MAX(D.PRD_EFCTV_DT)
  FROM WWPMARS.GPS_CNTRCT_LINE_ITM C, WWPMARS.GPS_CNTRCT_LI_PRD D
 WHERE     (    ( (C.CNTRCT_TYPE_CD = 'B') AND (C.CNTRCT_CTG_CD = 'ST'))
            AND (D.TOT_PRC_ADD_ON_PRC <> '0')
            and C.CNTRCT_NBR||C.CNTRCT_LI_NBR ='3VLZ0000')
       AND (D.PRD_EFCTV_DT <= CURRENT_DATE)
       AND C.CNTRCT_LI_NBR = D.CNTRCT_LI_NBR
       AND C.CNTRCT_NBR = D.CNTRCT_NBR 
       group by C.CNTRCT_NBR, C.CNTRCT_LI_NBR) MAINtbl
       on F.CNTRCT_NBR = C.CNTRCT_NBR 
       and F.CNTRCT_LI_NBR = MAINtbl.CNTRCT_LI_NBR ; -- changed here!