【问题标题】:select with subquery with filters in subquery在子查询中使用带有过滤器的子查询进行选择
【发布时间】:2015-10-13 20:28:08
【问题描述】:

我有以下代码:

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;

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

【问题讨论】:

  • 尝试格式化您的代码,以便人们阅读。显式join 有很大帮助。

标签: sql oracle join


【解决方案1】:

您可以使用

将查询放置在查询周围
    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

这是你要找的吗?

【讨论】:

  • from 子句是什么?
  • 整个查询没有 ; ?
  • 有人可以帮忙吗?
  • 问题出在哪里?有什么例外吗? (不应该;假设原始查询没问题)
  • 问题在于第二个子查询中的计数与放置在那里的过滤器的外部查询不相关。
猜你喜欢
  • 2010-10-11
  • 2011-11-06
  • 2020-06-13
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多