簡體   English   中英

SQL DB2 與連接運算符關聯或在 Merge 語句中的 On 子句無效

[英]SQL DB2 On clause associated with join operator or in a Merge statement is not valid

所以我試圖運行一個查詢並發現它產生了一個我不清楚的錯誤:

    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;

查詢非常接近工作,但由於某種原因,子選擇查詢拋出錯誤(如果我刪除它,我得到的結果基本上是正確的,除了不排除我想排除的那些結果)。 我通常在 db2 中看到 on 子句中的子選擇起作用,並且實際上有一些看起來相似的例子。

嘗試:

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

您的查詢似乎完全正確,但您仍然可以使用以下查詢 -

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;

如果你想在On Clause中過濾,你可以嘗試下面的查詢:

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;

如果您想排除所有具有service = 'TE'的 employer_id 以減少執行時間,那么您應該嘗試以下查詢:-

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM