Search

카테고리 별 도서 판매량 집계하기

SELECT BOOK.CATEGORY, sum(BOOK_SALES.SALES) as TOTAL_SALES from BOOK inner join BOOK_SALES on BOOK.BOOK_ID = BOOK_SALES.BOOK_ID where DATE_FORMAT(BOOK_SALES.SALES_DATE, '%Y-%m') between '2021-12' and '2022-02' group by BOOK.CATEGORY order by BOOK.CATEGORY
SQL
복사
위의 데이터는 2022-01 ~ 02 까지 조회

정답

like을 이용해 문자열 포함 여부를 따지는 것이 맞다
SELECT a.CATEGORY, sum(b.SALES) as TOTAL_SALES from BOOK as a JOIN BOOK_SALES as b on a.BOOK_ID = b.BOOK_ID where DATE_FORMAT(b.SALES_DATE, '%Y-%m') like '2022-01' group by a.CATEGORY order by a.CATEGORY
SQL
복사
SELECT BOOK.CATEGORY, sum(BOOK_SALES.SALES) as TOTAL_SALES from BOOK inner join BOOK_SALES on BOOK.BOOK_ID = BOOK_SALES.BOOK_ID where DATE_FORMAT(BOOK_SALES.SALES_DATE, '%Y-%m') between '2021-12' and '2022-01' group by BOOK.CATEGORY order by BOOK.CATEGORY
SQL
복사
between은 앞의 조건은 포함하지 않고 뒤의 조건은 포함한다