我需要帮助来重写用联合加入联接编写的SQL查询

时间:2018-10-01 18:09:28

标签: sql oracle

这是查询

SELECT
    FD_DESC_CD, 
    FD_NM, 
    FD_CUSIP_CD, 
    NASDAQ_SYM 
FROM 
    DBSYSTP.FP_FUND_DESC a, DBSYSTC.funddesc b 
WHERE 
    a.FD_ACTIVE_CD = 1 
    AND a.FD_DESC_CD = b.FD_DESCR_CODE 
    AND a.FD_GROUP_CD = 'OF' 
    AND FD_STYLE_C <> 11 
    AND a.OUTSIDE_FD_TYPE_CD IN ('LIP','NFIA','NFPA')  
    AND (a.FD_DESC_CD NOT IN ('Y3RN','N699')) 

UNION 

SELECT 
    FD_DESC_CD, 
    FD_NM, 
    FD_CUSIP_CD, 
    NASDAQ_SYM 
FROM 
    DBSYSTP.FP_FUND_DESC a, DBSYSTT.funddesc b 
WHERE
    a.FD_ACTIVE_CD = 1 
    AND a.FD_DESC_CD = b.FD_DESCR_CODE 
    AND a.FD_GROUP_CD = 'OF' 
    AND FD_STYLE_C <> 11 
    AND a.OUTSIDE_FD_TYPE_CD IN ('LIP','NFIA','NFPA')  
    AND (a.FD_DESC_CD NOT IN ('Y3RN','N699')) 

UNION 

SELECT 
    FD_DESC_CD, 
    FD_NM, 
    FD_CUSIP_CD, 
    NASDAQ_SYM 
FROM 
    DBSYSTP.FP_FUND_DESC a, DBSYSTI.funddesc b 
WHERE 
    a.FD_ACTIVE_CD = 1 
    AND a.FD_DESC_CD = b.FD_DESCR_CODE 
    AND a.FD_GROUP_CD = 'OF' 
    AND FD_STYLE_C <> 11 
    AND a.OUTSIDE_FD_TYPE_CD IN ('LIP','NFIA','NFPA')  
    AND (a.FD_DESC_CD NOT IN ('Y3RN','N699')) 

结果应采用以下格式:。

  FD_DESC_CD    FD_NM              FD_CUSIP_CD  NASDAQ_SYM
    CC07    Blackrock Liquidity     09248U718   EIIPX
    CC12    Invesco American        00141A446   GSTIX
    CC15    J.P. Morgan             4812C2577   EIIGX

1 个答案:

答案 0 :(得分:0)

您可以使用子查询并将其加入

     with t1 as
        (
         Select FD_DESC_CD, FD_NM, FD_CUSIP_CD, NASDAQ_SYM from DBSYSTP.FP_FUND_DESC a, DBSYSTC.funddesc b WHERE a.FD_ACTIVE_CD = 1 and a.FD_DESC_CD = b.FD_DESCR_CODE and a.FD_GROUP_CD = 'OF' and FD_STYLE_C <> 11 and a.OUTSIDE_FD_TYPE_CD in ('LIP','NFIA','NFPA')  and (a.FD_DESC_CD not in ('Y3RN','N699')) 
        ), t2 as
        ( 
          Select FD_DESC_CD, FD_NM, FD_CUSIP_CD, NASDAQ_SYM from DBSYSTP.FP_FUND_DESC a, DBSYSTT.funddesc b WHERE a.FD_ACTIVE_CD = 1 and a.FD_DESC_CD = b.FD_DESCR_CODE and a.FD_GROUP_CD = 'OF' and FD_STYLE_C <> 11 and a.OUTSIDE_FD_TYPE_CD in ('LIP','NFIA','NFPA')  and (a.FD_DESC_CD not in ('Y3RN','N699')) 

        ) , 
     t3 as (    
    Select FD_DESC_CD, FD_NM, FD_CUSIP_CD, NASDAQ_SYM from DBSYSTP.FP_FUND_DESC a, DBSYSTI.funddesc b WHERE a.FD_ACTIVE_CD = 1 and a.FD_DESC_CD = b.FD_DESCR_CODE and a.FD_GROUP_CD = 'OF' and FD_STYLE_C <> 11 and a.OUTSIDE_FD_TYPE_CD in ('LIP','NFIA','NFPA')  and (a.FD_DESC_CD not in ('Y3RN','N699'))
        ) 
select t1.*,t2.*,t3.* from t1 join t2 on t1.FD_DESC_CD=t2.FD_DESC_CD
          join t3 on t1.FD_DESC_CD=t3.FD_DESC_CD