데이터 교육/SQL
[SQL] 12. 사칙연산(할인률, 판매가, 이익률) 계산하기
마크잉
2023. 5. 29. 21:17
출처: 패스트 캠퍼스 - 한 번에 끝내는 데이터 분석 초격차 패키지 Online.
사칙연산 함수
discount profit
00) 기본단위
* 할인금액: discount
* 매출액: gmv
* 상품별 이익: product_profit
* 전체 이익: total_profit
01) 할인률: discount / gmv
*쿼리 작성
select *,
cast (discount as numeric) / gmv as discount_rate >>> 정수 / 정수 = 정수, 즉 cast를 이용해 소수점으로 바꿔줘야합니다
from online_order oo
02) 판매가 : gmv - discount
*쿼리 작성
select *,
gmv - discount as paid_amount
from online_order oo

03) 이익률: product_profit / gmv, total_profit / gmv
*상품별 이익률
select *,
cast (product_profit as numeric) / gmv as product_margin
from online_order oo
*전체 이익률
select *,
cast (total_profit as numeric) / gmv as total_margin
from online_order oo