SQL 알고리즘 코드카타

SQL 알고리즘 코드카타 22일차(1) 26.01.20(화)

nom_de_plume 2026. 1. 20. 09:26

부제:

저자 별 카테고리 별 매출액 집계하기, 레벨 5

 

1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/144856
2. 정답 코드: 

SELECT
    b.author_id,
    a.author_name,
    b.category,
    sum(bs.sales * b.price) as total_sales
from book as b
join author as a
    on b.author_id = a.author_id
join book_sales as bs
    on b.book_id = bs.book_id
where sales_date like ("2022-01-%")
group by b.author_id, b.category
order by b.author_id asc, b.category desc


3. 오류 상황:

SELECT
    b.author_id,
    a.author_name,
    b.category,
    sum(bs.sales * b.price) as total_sales
from book as b
join author as a
    on b.author_id = a.author_id
join book_sales as bs
    on b.book_id = bs.book_id
where sales_date like ("2022-01-%")
group by b.category
order by b.author_id asc, b.category desc

=> GROUP BY 기준 오류


4. 시도 방법:

=> GROUP BY 기준 수정


5. 최종 문제 해결 방법:

=> 문제에서 요구한 최종 출력 형태는 "저자 별, 카테고리 별"이다. 즉, 같은 저자가 쓴 같은 카테고리의 책이 여러 권 있더라도 하나로 합쳐져야 한다. GROUP BY 절에 AUTHOR_ID와 CATEGORY를 포함하여 그룹을 다시 정의.