SQL Oracle子查询

时间:2018-10-12 13:02:21

标签: subquery teradata

我在运行此子查询时遇到问题。我正在使用Toad Data Point -Oracle。我收到语法错误。我已经尝试了几种不同的方法,但是没有运气。我知道要子查询的

Select *
from FINC.VNDR_ITEM_M as M
where M.ACCT_DOC_NBR = A.ACCT_DOC_NBR
(SELECT A.CLIENT_ID,
       A.SRC_SYS_ID,
       A.CO_CD,
       A.ACCT_NBR,
       A.CLR_DT,
       A.ASGN_NBR,
       A.FISCAL_YR,
       A.ACCT_DOC_NBR,
       A.LINE_ITEM_NBR,
       A.MFR_PART_NBR,
       A.POST_DT,
       A.DRCR_IND, 
       A.DOC_CRNCY_AMT, 
       A.CRNCY_CD, 
       A.BSL_DT        
FROM FINC.VNDR_ITEM_F A
WHERE A.CLR_DT IN (SELECT MAX(B.CLR_DT)
                  FROM FINC.VNDR_ITEM_F AS B
                  where (B.ACCT_DOC_NBR = A.ACCT_DOC_NBR and B.FISCAL_YR=A.FISCAL_YR and B.LINE_ITEM_NBR = A.LINE_ITEM_NBR and B.SRC_SYS_ID =A.SRC_SYS_ID and B.POST_DT=A.POST_DT and B.CO_CD=A.CO_CD) 
                  and (B.CO_CD >='1000' and  B.CO_CD <= '3000'  or B.CO_CD ='7090') and (B.POST_DT Between to_date ('08/01/2018','mm/dd/yyyy')
                                        AND to_date ('08/31/2018', 'mm/dd/yyyy')) and (B.SRC_SYS_ID ='15399') and (B.FISCAL_YR ='2018'))
GROUP BY 
       A.CLIENT_ID,
       A.SRC_SYS_ID,
       A.CO_CD,
       A.ACCT_NBR,
       A.CLR_DT,
       A.ASGN_NBR,
       A.FISCAL_YR,
       A.ACCT_DOC_NBR,
       A.LINE_ITEM_NBR,
       A.MFR_PART_NBR,
       A.POST_DT,
       A.DRCR_IND, 
       A.DOC_CRNCY_AMT, 
       A.CRNCY_CD,
       A.BSL_DT)

2 个答案:

答案 0 :(得分:1)

您的语法已损坏,请将子查询放在最后。现在看起来像:

select *
from dual as m 
where a.dummy = m.dummy
(select dummy from dual)

它在错误的位置,没有加入,没有别名。您可能应该做的是:

select *
from dual m 
join (select dummy from dual) a on a.dummy = m.dummy

您也有一些多余的不必要的括号,但这是次要的缺陷。完整代码(如果没有数据访问,我将无法测试):

select *
from FINC.VNDR_ITEM_M M
join (SELECT A.CLIENT_ID, A.SRC_SYS_ID, A.CO_CD, A.ACCT_NBR, A.CLR_DT, A.ASGN_NBR,
               A.FISCAL_YR, A.ACCT_DOC_NBR, A.LINE_ITEM_NBR, A.MFR_PART_NBR, A.POST_DT,
               A.DRCR_IND, A.DOC_CRNCY_AMT, A.CRNCY_CD, A.BSL_DT
          FROM FINC.VNDR_ITEM_F A
          WHERE A.CLR_DT IN (SELECT MAX(B.CLR_DT)
                               FROM FINC.VNDR_ITEM_F AS B
                               where B.ACCT_DOC_NBR = A.ACCT_DOC_NBR 
                                 and B.FISCAL_YR=A.FISCAL_YR 
                                 and B.LINE_ITEM_NBR = A.LINE_ITEM_NBR 
                                 and B.SRC_SYS_ID =A.SRC_SYS_ID 
                                 and B.POST_DT=A.POST_DT 
                                 and B.CO_CD=A.CO_CD 
                                 and (('1000'<=B.CO_CD and B.CO_CD<='3000') or B.CO_CD='7090') 
                                 and B.POST_DT Between to_date ('08/01/2018', 'mm/dd/yyyy')
                                                   AND to_date ('08/31/2018', 'mm/dd/yyyy') 
                                 and B.SRC_SYS_ID ='15399' and B.FISCAL_YR ='2018')
          GROUP BY A.CLIENT_ID, A.SRC_SYS_ID, A.CO_CD, A.ACCT_NBR, A.CLR_DT, A.ASGN_NBR, 
                   A.FISCAL_YR, A.ACCT_DOC_NBR, A.LINE_ITEM_NBR, A.MFR_PART_NBR, A.POST_DT, 
                   A.DRCR_IND, A.DOC_CRNCY_AMT, A.CRNCY_CD, A.BSL_DT) A
    on M.ACCT_DOC_NBR = A.ACCT_DOC_NBR and M.CO_CD=A.CO_CD;

答案 1 :(得分:0)

您需要为SubSelect添加别名(或在标准SQL中为派生表):

select *
from
 ( select .......
 ) AS dt
join ....