与join运算符关联的sql db2 on子句或merge语句中的sql db2 on子句无效

cbjzeqam  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(415)

所以我试着运行一个查询,发现它产生了一个我不清楚的错误:

select a.employer_id, a.customer_id, s.service, count(a.account_num) as "TOTAL_COUNT" 
        from dhs01.msa_account  a
left outer join dhs01.alt_services s
    on a.customer_id = s.customer_id
        and s.service = 'TH'
        and a.employer_id not in (select s2.employer_id from dhs01.alt_services s2 where s2.service = 'TE')   -<<<<----- THIS IS THE BAD LINE
where 
        a.ACCT_MAINT_FEE = 'E'
        AND a.STAT = 'O'
        AND a.OPEN_CONFIRM_DATE < (CURRENT_DATE - (DAY(CURRENT_DATE)-1) DAYS) ---first of the current 

    group by a.employer_id, a.customer_id, s.service;

查询非常接近工作状态,但是由于某种原因,subselect查询抛出了一个错误(如果我删除了这个错误,我得到的结果基本上是正确的,除了不排除那些我想排除的结果)。我通常看到on子句中的subselect在db2中起作用,而且实际上有一些示例与之类似。

jyztefdp

jyztefdp1#

尝试:

from dhs01.msa_account  a
left join table
(
select s.*
from dhs01.alt_services s 
where s.customer_id = a.customer_id and s.service = 'TH'
and not exists
(
select 1
from dhs01.alt_services s2 
where s2.service = 'TE' and s2.employer_id = a.employer_id
)
) s on 1=1

而不是:

from dhs01.msa_account  a
left outer join dhs01.alt_services s
    on a.customer_id = s.customer_id
        and s.service = 'TH'
        and a.employer_id not in (select s2.employer_id from dhs01.alt_services s2 where s2.service = 'TE')   -<<<<----- THIS IS THE BAD LINE
7lrncoxx

7lrncoxx2#

您的查询似乎完全正确,但仍然可以使用下面的查询-

SELECT          a.employer_id, 
                a.customer_id, 
                s.service, 
                COUNT(a.account_num) AS "TOTAL_COUNT" 
FROM            dhs01.msa_account a 
LEFT OUTER JOIN dhs01.alt_services s 
ON              a.customer_id = s.customer_id 
AND             s.service = 'TH' 
WHERE           a.acct_maint_fee = 'E' 
AND             a.stat = 'O' 
AND             a.open_confirm_date < (CURRENT_DATE - (DAY(CURRENT_DATE)-1) DAYS) --first of the current 
AND NOT EXISTS  (SELECT NULL
                 FROM   dhs01.alt_services s2 
                 WHERE  s2.service = 'TE'
                 AND    a.employer_id = s2.employer_id)
GROUP BY        a.employer_id, 
                a.customer_id, 
                s.service;
fzsnzjdm

fzsnzjdm3#

如果要在on子句中进行筛选,可以尝试以下查询:

select a.employer_id, a.customer_id, s.service, count(a.account_num) as "TOTAL_COUNT" 
    from dhs01.msa_account  a
left outer join dhs01.alt_services s
on a.customer_id = s.customer_id
    and s.service = 'TH'
    and a.employer_id <> (select distinct s2.employer_id from dhs01.alt_services s2 where s2.service = 'TE' and a.employer_id=s2.employer_id)   
where 
    a.ACCT_MAINT_FEE = 'E'
    AND a.STAT = 'O'
    AND a.OPEN_CONFIRM_DATE < (CURRENT_DATE - (DAY(CURRENT_DATE)-1) DAYS) ---first of the current 

group by a.employer_id, a.customer_id, s.service;

如果您想排除所有服务为'te'的雇主标识,从而减少执行时间,那么您应该尝试下面的方法query:-

select a.employer_id, a.customer_id, s.service, count(a.account_num) as "TOTAL_COUNT" 
    from dhs01.msa_account  a
left outer join dhs01.alt_services s
on a.customer_id = s.customer_id
    and s.service = 'TH'   
where 
    a.ACCT_MAINT_FEE = 'E'
    AND a.STAT = 'O'
    AND a.OPEN_CONFIRM_DATE < (CURRENT_DATE - (DAY(CURRENT_DATE)-1) DAYS) ---first of the current 
AND not exists(select 1 from dhs01.alt_services s2 where s2.service = 'TE' and a.employer_id=s2.employer_id)

group by a.employer_id, a.customer_id, s.service;

相关问题