아래 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을 분모분자 묶어주니 다시 결과가 실행이되네요;;
'IT_SQL' 카테고리의 다른 글
[분석 기초] SQL 데이터 유형 총정리(문자형, 숫자형, 날짜형, 참/거짓, 변환) (0) | 2021.11.17 |
---|---|
[분석실습] SQL 데이터 사칙연산 심화 (날짜변환, 연산, 고유값) (0) | 2021.11.16 |
[분석실습] SQL 데이터 사칙연산 (round, 소수점, 반올림, 퍼센트~) (0) | 2021.11.14 |
[분석실습] SQL 데이터 날짜함수 가공하기(now, date_part, current, interval ~) (0) | 2021.11.12 |
Question) case when 절 IN() + like %~% 겹쳐쓰기 불가능? (0) | 2021.11.12 |