왼쪽 외부 조인에 대한 case 문을 사용하여이 SQL 문을 최적화 할 수 있다고 생각합니다.SQL 최적화 사례 문
하지만 저는 AB, CD 및 다른 모든 코드 유형을 요약하기 위해 사례를 설정하는 데 어려움을 겪고 있습니다.
저에게 줄 수있는 도움이나 조언을 보내 주시면 감사하겠습니다.
update billing set payments = isnull(bd1.amount, payments)
, payments = case
when payments is null then 0
else payments
end
, charges = case
when bd2.amount is not null then charges
when charges is null then 0
else charges
end
, balance = round(charges + isnull(bd1.amount, bi.payments), 2)
from billing bi
left outer join (select inv, round(sum(bd1.bal), 2) amount
from "bill" bd1
where code_type = 'AB'
or code_type = 'CD'
group by inv) bd1
on bd1.inv = bi.inv
left outer join (select invoice, round(sum(bd2.bal), 2) amount
from "bill" bd2
where code_type <> 'AB'
and code_type <> 'CD'
group by inv) bd2
on bd2.inv = bi.inv;
유효합니까? 'payments' 칼럼을 두 번 업데이트하는 것 같습니다. –
@ 시바 나는 이점 9를 사용하고 있습니다 – Trevor