[프로그래머스] 저자 별 카테고리 별 매출액 집계하기

yenpkr·2025년 3월 2일
0

sql

목록 보기
39/91

문제

제출

SELECT b.author_id,b.author_name,a.category,sum(a.price*c.sales) TOTAL_SALES
from book a
join author b
on a.author_id = b.author_id
join book_sales c
on a.book_id = c.book_id
where date_format(c.sales_date,'%Y-%m') = '2022-01'
group by 1,3
order by 1 asc,3 desc

🚨 error

SELECT b.author_id,b.author_name,a.category,a.price*sum(c.sales) TOTAL_SALES
from book a
join author b
on a.author_id = b.author_id
join book_sales c
on a.book_id = c.book_id
where date_format(c.sales_date,'%Y-%m') = '2022-01'
group by 1,3
order by 1 asc,3 desc

a.price*sum(c.sales) TOTAL_SALES 이 부분때문에 계속 에러가 났다.

예를 들어 이 예제 데이터에서, a.price*sum(c.sales) TOTAL_SALES 로 작성하면 1000*8 인지 2000*8인지 알 수 없다.
따라서 sum(a.price*c.sales) TOTAL_SALES 처럼 SUM을 전체에 묶어줘야 한다.

0개의 댓글