반응형
SMALL
select c.cate1 ,
round(sum(cast(discount as numeric)) / sum(gmv) *100 , 0) || '%' as discount_rate,
sum(gmv - discount) as paid_amount,
round(sum(cast(product_profit as numeric)) / sum(gmv) *100, 0) ||'%' as product_margin,
round(sum(cast(total_profit as numeric)) / sum(gmv) *100 , 0) || '%' as total_margin
from online_order oo
join item i on oo.itemid = i.id
join category c on i.category_id = c.id
group by 1
order by 3 desc
;
as 중요
cast(~ as varchar) -> 숫자를 문자로
cast(~ as numeric) -> 소수 변환
분자 분모 sum으로 묶어야함.
round 는 절삭을 위해 사용 -> ,0 안붙여도댐
||'%' 뒤에 %를 붙이기 위해
반응형
LIST
'SQL > SQL Note' 카테고리의 다른 글
조건 (0) | 2023.08.17 |
---|---|
날짜 관련 함수 (0) | 2023.08.17 |
데이터 유형 변환, 데이터 가공 (0) | 2023.08.17 |
count(*), count(age) (0) | 2023.08.17 |
LIKE '%~%', Group by (0) | 2023.08.17 |