IT_SQL

Question) join 후 sum 오류

soyounism 2021. 11. 14. 20:04

아래 1번과 2번에서 달라진 것은 join의 차이입니다

조인해주니 0으로는 나눌 수 없다고나오네요?;;....

 

왜 그런 걸까요..?

 

1.조인 없이 sum 계산

-sum을 '0/0' 분모 분자 따로해도, 묶어줘도 (값은 다르지만) 계산 정상 

select 
buyer_nm 
, sum(cast(dis_amt as numeric)) / sum(tot_amt) as discount_rate
from hds_transactions_rea_csv htrc
group by 1;

 

2. 조인 후 오류

select 
buyer_nm
, sum(cast(htrc.dis_amt as numeric)) / sum(htrc.tot_amt) as discount_rate
from hds_transactions_rea_csv htrc 
join hds_customers_rea_csv hcrc on htrc.custid = hcrc.custid 
group by 1

 

 

* 추가

2번 오류에서 분모분자 각각 sum의 경우는 오류가 나는데,

sum을 분모분자 묶어주니 다시 결과가 실행이되네요;;