使用子查询选择子查询中的过滤器

时间:2015-10-13 20:28:09

标签: sql oracle join

Current output(wrong) Business requirement

我有以下代码:

SELECT DISTINCT    
    tctc_cntipcli as "Type of contract" ,
    texe_cncclipu as "Contract number",
    TCTC_CNDOCIDC as "Client name", 
    tsrv_cndesser as "Service name", 
    texe_cnfuncid as "Service number", 
    tsrs_cnsubsdc as "Subservice name", 
    texe_cnsubser as "Subservice number", 
    tmap_cndesc as "Map",

    ( 
        SELECT to_char(count(tlof_cnlofrid)) 
        from service.kndtlof
        where   
            tlof_cncclipu = tctc_cncclipu and
            tlof_cnservic = texe_cnfuncid and
            tlof_cnsubser = texe_cnsubser and
            tlof_cnfhalta > trunc (sysdate, 'mm')
    ) as "Volume of files messages" ,
    (
        select count(it.int_tran_id)
        from internal_transactions it
        join status ss on ss.int_tran_id = it.int_tran_id
        join app_data ad on it.int_tran_id in 
        (
            select distinct ad.int_tran_id
            from app_data
            where app_data.add_info_table in ('ACH', 'Wire')
        )
        join service.kndtexe ke on it.debit_acct in 
        (
            select distinct texe_cnasupro
            from service.kndtexe ke
            where 
                (
                    (ke.texe_cnfuncid = '3001' and ke.texe_cnsubser = 'W1')
                    or
                    (ke.texe_cnfuncid = '3085' and ke.texe_cnsubser = 'I%')
                )
        )
        where it.entry_timestamp > trunc(sysdate, 'mm')
    ) as "Volume of payments"
from 
    service.kndtctc, 
    service.kndtexe, 
    service.kndtscm, 
    service.kndtsrv, 
    service.kndtsrs, 
    service.kndtmap
where 
    tctc_cncclipu = texe_cncclipu
    and texe_cnfuncid = tsrv_cncveser
    and texe_cnfuncid = tsrs_cncveser 
    and texe_cnsubser = tsrs_cnsubser
    and texe_cncclipu = tscm_cncontra
    and tscm_cnmapco = tmap_cnmapco
    and tscm_cnservic = tsrv_cncveser
    and tscm_cnsubser = tsrs_cnsubser
    and tctc_cnestado in ('01', '03')
    and texe_cnestado in ('01', '03')
    and tsrv_cnestado in ('01', '03')
    and tsrs_cnestado in ('01', '03')
    and tscm_cnestado in ('01', '03')
    and tmap_cnestado in ('01', '03')
    and tctc_cncclipu in ('50008753')
order by tctc_cncclipu;

第二个子查询是从事务数据库查询到服务数据库(请参阅业务需求图像),并且需要检查texe表中是否存在与tctc中的合同编号下的合同关联的任何帐户,并仅输出3001 / W1或3085 / I%。如何过滤子查询来实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以使用

围绕查询
    SELECT * FROM
    (
------------------
SELECT DISTINCT    tctc_cntipcli as "Type of contract" ,
                       texe_cncclipu as "Contract number",
                       TCTC_CNDOCIDC as "Client name", 
                       tsrv_cndesser as "Service name", 
                       texe_cnfuncid as "Service number", 
                       tsrs_cnsubsdc as "Subservice name", 
                       texe_cnsubser as "Subservice number", 
                       tmap_cndesc as "Map",

           ( 
            SELECT to_char(count(tlof_cnlofrid)) 
            from service.kndtlof
            where   tlof_cncclipu = tctc_cncclipu
            and     tlof_cnservic = texe_cnfuncid
            and     tlof_cnsubser = texe_cnsubser
            and     tlof_cnfhalta > trunc (sysdate, 'mm')
           ) as "Volume of files messages" ,

         (select count(it.int_tran_id)
         from internal_transactions it
         join status ss on ss.int_tran_id = it.int_tran_id
         join app_data ad on it.int_tran_id in 
(select distinct ad.int_tran_id                                                                               from app_data
where app_data.add_info_table in ('ACH', 'Wire')
)
join service.kndtexe ke on it.debit_acct in 
(select distinct texe_cnasupro
from service.kndtexe ke
where ( 
(ke.texe_cnfuncid = '3001' and ke.texe_cnsubser = 'W1')
or
(ke.texe_cnfuncid = '3085' and ke.texe_cnsubser = 'I%')
)
                                                                                         )
where it.entry_timestamp > trunc(sysdate, 'mm')
                                           ) as "Volume of payments"

from service.kndtctc, service.kndtexe, service.kndtscm, service.kndtsrv, service.kndtsrs, service.kndtmap
                              where tctc_cncclipu = texe_cncclipu
                              and texe_cnfuncid = tsrv_cncveser
                              and texe_cnfuncid = tsrs_cncveser 
                              and texe_cnsubser = tsrs_cnsubser
                              and texe_cncclipu = tscm_cncontra
                              and tscm_cnmapco = tmap_cnmapco
                              and tscm_cnservic = tsrv_cncveser
                              and tscm_cnsubser = tsrs_cnsubser
                              and tctc_cnestado in ('01', '03')
                              and texe_cnestado in ('01', '03')
                              and tsrv_cnestado in ('01', '03')
                              and tsrs_cnestado in ('01', '03')
                              and tscm_cnestado in ('01', '03')
                              and tmap_cnestado in ('01', '03')
                              and tctc_cncclipu in ('50008753')
                              order by tctc_cncclipu
------------------
    ) WHERE "Volume of payments" > 0

这是你在找什么?

相关问题