Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ €์ž๋ณ„ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๋งค์ถœ์•ก ์ง‘๊ณ„ํ•˜๊ธฐ (23.06.13 ํ’€์ด ์ถ”๊ฐ€)

์ธ” 2022. 12. 22. 15:20

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

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

 


โ–  ๋ฌธ์ œ

๋ฌธ์ œ์„ค๋ช…

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

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 ์ถœํŒ์ผ

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

Column name Type Nullable Description
AUTHOR_ID INTEGER FALSE ์ €์ž ID
AUTHOR_NAME VARCHAR(N) FALSE ์ €์ž๋ช…

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

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

 


๋ฌธ์ œ

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


โ–  ํ’€์ด

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

โ‘  2022๋…„ 1์›”์˜ ๋„์„œ ํŒ๋งค ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ                                                    > WHERE ์กฐ๊ฑด1

โ‘ก ์ €์ž๋ณ„¹, ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„² , ๋งค์ถœ์•ก(TOTAL_SALES = ํŒ๋งค๋Ÿ‰ * ํŒ๋งค๊ฐ€)           >  ๊ทธ๋ฃน์กฐ๊ฑด 1, 2  → ์ง‘๊ณ„ํ•จ์ˆ˜

                                                                                                                                             + ์ปฌ๋Ÿผ ๊ฐ„ ์—ฐ์‚ฐ (ํŒ๋งค๋Ÿ‰ × ํŒ๋งค๊ฐ€)

 

โ‘ข ์ €์ž ID(AUTHOR_ID), ์ €์ž๋ช…(AUTHOR_NAME), ์นดํ…Œ๊ณ ๋ฆฌ(CATEGORY),  ๋งค์ถœ์•ก(SALES) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ

> โ‘ , โ‘ก ์ˆ˜ํ–‰ ์ „ BOOK, AUTHOR, BOOK_SALES ํ…Œ์ด๋ธ” ๊ฐ„ ์กฐ์ธํ•ด์•ผ WHERE์ ˆ ์กฐ๊ฑด, ๊ทธ๋ฃนํ•‘, ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ๊ฐ„ ์—ฐ์‚ฌ์ด๊ฐ€๋Šฅํ•˜๋‹ค. 

 

์ €์ž ID๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ¹์œผ๋กœ, ์ €์ž ID๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ² ์ •๋ ฌ           > ์ •๋ ฌ์กฐ๊ฑด1, 2

 

SELECT BO.AUTHOR_ID, AU.AUTHOR_NAME, BO.CATEGORY, SUM(BS.SALES * PRICE) AS TOTAL_SALES
 FROM BOOK BO
 
 INNER JOIN AUTHOR AU
 ON BO.AUTHOR_ID = AU.AUTHOR_ID      -- BOOK ํ…Œ์ด๋ธ”๊ณผ AUTHOR ํ…Œ์ด๋ธ” ์กฐ์ธ
 
 INNER JOIN BOOK_SALES BS
 ON BO.BOOK_ID = BS.BOOK_ID          -- BOOK_SALES ํ…Œ์ด๋ธ”๊ณผ BOOK ํ…Œ์ด๋ธ” ์กฐ์ธ
 
 WHERE DATE_FORMAT(BS.SALES_DATE, '%Y-%m-%d') LIKE '2022-01-%'  -- ํŒ๋งค์›” 22๋…„ 1์›” ํ•œ์ • ์กฐ๊ฑด ๋ถ€์—ฌ
 GROUP BY 2, 3                                                  -- ์ €์ž๋ณ„, ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๊ทธ๋ฃนํ•‘
 ORDER BY 1, 3 DESC                                             -- ์ €์ž ID๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ, 
                                                                -- ์ €์ž ID๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

 

โœ” ์žฌํ’€์ด (2023.06.13)

 ์žฌํ’€์ด์—์„œ๋Š” BOOK_SALES ํ…Œ์ด๋ธ”์—์„œ 2022-01 ํŒ๋งค๋Ÿ‰๋งŒ ๊ณ ๋ คํ•˜๋Š” ๋งŒํผ

ํ…Œ์ด๋ธ” ๊ฐ„ ์กฐ์ธ ์ „์— ๋ฐ์ดํ„ฐ๋ฅผ ํ•„ํ„ฐ๋งํ•ด์„œ B3๋กœ ์กฐ์ธํ–ˆ๋‹ค.

SELECT B2.AUTHOR_ID, B2.AUTHOR_NAME, B1.CATEGORY, SUM(B1.PRICE * B3.SALES) AS TOTAL_SALES
 FROM BOOK B1
 LEFT JOIN AUTHOR B2
 ON B1.AUTHOR_ID = B2.AUTHOR_ID
 
 INNER JOIN (SELECT * FROM BOOK_SALES 
              WHERE DATE_FORMAT(SALES_DATE, '%Y-%m')='2022-01') B3
 ON B1.BOOK_ID = B3.BOOK_ID
 
 GROUP BY 1, 3
 ORDER BY 1, 3 DESC;