Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๋„์„œ ํŒ๋งค๋Ÿ‰ ์ง‘๊ณ„ํ•˜๊ธฐ

์ธ” 2022. 12. 22. 14:58

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต,   
      https://school.programmers.co.kr/learn/challenges?page=1&languages=mysql%2Coracle
์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

 


โ–  ๋ฌธ์ œ

๋ฌธ์ œ์„ค๋ช…

๋‹ค์Œ์€ ์–ด๋Š ํ•œ ์„œ์ ์—์„œ ํŒ๋งค์ค‘์ธ ๋„์„œ๋“ค์˜ ๋„์„œ ์ •๋ณด(BOOK), ํŒ๋งค ์ •๋ณด(BOOK_SALES) ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค.

BOOK ํ…Œ์ด๋ธ”์€ ๊ฐ ๋„์„œ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

Column name Type Nullable Description
BOOK_ID INTEGER FALSE ๋„์„œ ID
CATEGORY VARCHAR(N) FALSE ์นดํ…Œ๊ณ ๋ฆฌ (๊ฒฝ์ œ, ์ธ๋ฌธ, ์†Œ์„ค, ์ƒํ™œ, ๊ธฐ์ˆ )
AUTHOR_ID INTEGER FALSE ์ €์ž ID
PRICE INTEGER FALSE ํŒ๋งค๊ฐ€ (์›)
PUBLISHED_DATE DATE FALSE ์ถœํŒ์ผ

BOOK_SALES ํ…Œ์ด๋ธ”์€ ๊ฐ ๋„์„œ์˜ ๋‚ ์งœ ๋ณ„ ํŒ๋งค๋Ÿ‰ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

Column name Type Nullable Description
BOOK_ID INTEGER FALSE ๋„์„œ ID
SALES_DATE DATE FALSE ํŒ๋งค์ผ
SALES INTEGER FALSE ํŒ๋งค๋Ÿ‰

 

๋ฌธ์ œ

2022๋…„ 1์›”์˜ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋„์„œ ํŒ๋งค๋Ÿ‰์„ ํ•ฉ์‚ฐํ•˜๊ณ , ์นดํ…Œ๊ณ ๋ฆฌ(CATEGORY), ์ด ํŒ๋งค๋Ÿ‰(TOTAL_SALES) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ์นดํ…Œ๊ณ ๋ฆฌ๋ช…์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.


โ–  ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

2022๋…„ 1์›”์˜ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋„์„œ ํŒ๋งค๋Ÿ‰์„ ํ•ฉ์‚ฐํ•˜๊ณ ,                                     > ํŒ๋งค์›”๋ณ„ ์ง‘๊ณ„ํ•จ์ˆ˜ ๊ทธ๋ฃนํ•‘

                                                                                                                                   > ๋ฌธ์ œ ์˜ˆ์‹œ์— ๋‚ ์งœ ์กฐ๊ฑด์€ DATE_FORMAT()ํ•จ์ˆ˜๋‚˜ ํŠน์ • ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด ์ถ”์ถœ LIKE๋กœ ์กฐ๊ฑด ๋ถ€์—ฌ

์นดํ…Œ๊ณ ๋ฆฌ(CATEGORY), ์ด ํŒ๋งค๋Ÿ‰(TOTAL_SALES) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ     > ๊ทธ๋ฃนํ•‘ ์ „์— ์นดํ…Œ๊ณ ๋ฆฌ(CATEGORY) ์ •๋ณด๋Š” BOOKํ…Œ์ด๋ธ”์— ์žˆ๊ณ ,  ํŒ๋งค๋Ÿ‰(SALES) ์ •๋ณด๋Š” BOOK_SALESํ…Œ์ด๋ธ”์— ์žˆ์œผ๋ฏ€๋กœ ๊ณตํ†ต ์ปฌ๋Ÿผ(BOOK_ID)๋ฅผ ๊ธฐ์ค€์œผ๋กœ JOIN ํ•ด์ฃผ์–ด์•ผ ํ•จ.

 

๊ฒฐ๊ณผ๋Š” ์นดํ…Œ๊ณ ๋ฆฌ๋ช…์„ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ                                                         > ์ •๋ ฌ๊ธฐ์ค€1

SELECT BO.CATEGORY, SUM(BS.SALES) AS TOTAL_SALES
 FROM BOOK BO
 INNER JOIN BOOK_SALES BS
 ON BO.BOOK_ID = BS.BOOK_ID
 WHERE BS.SALES_DATE LIKE '2022-01-%'
 GROUP BY 1
 ORDER BY 1

* WHERE์ ˆ์˜ ํŒ๋งค์ผ ์กฐ๊ฑด์€ WHERE DATE_FORMAT(BS.SALES_DATE, '%Y-%m-%d') LIKE '2022-01-%' ๋„ ๊ฐ€๋Šฅํ–ˆ๋‹ค.