Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ƒํ’ˆ๋ณ„ ์˜คํ”„๋ผ์ธ ๋งค์ถœ ๊ตฌํ•˜๊ธฐ

์ธ” 2022. 12. 16. 18:33

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

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

 


โ–  ๋ฌธ์ œ

๋ฌธ์ œ์„ค๋ช…

๋‹ค์Œ์€ ์–ด๋Š ์˜๋ฅ˜ ์‡ผํ•‘๋ชฐ์—์„œ ํŒ๋งค์ค‘์ธ ์ƒํ’ˆ๋“ค์˜ ์ƒํ’ˆ ์ •๋ณด๋ฅผ ๋‹ด์€ PRODUCT ํ…Œ์ด๋ธ”๊ณผ ์˜คํ”„๋ผ์ธ ์ƒํ’ˆ ํŒ๋งค ์ •๋ณด๋ฅผ ๋‹ด์€ OFFLINE_SALE ํ…Œ์ด๋ธ” ์ž…๋‹ˆ๋‹ค. PRODUCT ํ…Œ์ด๋ธ”์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ PRODUCT_ID, PRODUCT_CODE, PRICE๋Š” ๊ฐ๊ฐ ์ƒํ’ˆ ID, ์ƒํ’ˆ์ฝ”๋“œ, ํŒ๋งค๊ฐ€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

Column name Type Nullable
PRODUCT_ID INTEGER FALSE
PRODUCT_CODE VARCHAR(8) FALSE
PRICE INTEGER FALSE

์ƒํ’ˆ ๋ณ„๋กœ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 8์ž๋ฆฌ ์ƒํ’ˆ์ฝ”๋“œ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ์•ž 2์ž๋ฆฌ๋Š” ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

OFFLINE_SALE ํ…Œ์ด๋ธ”์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ๋˜์–ด์žˆ์œผ๋ฉฐ OFFLINE_SALE_ID, PRODUCT_ID, SALES_AMOUNT, SALES_DATE๋Š” ๊ฐ๊ฐ ์˜คํ”„๋ผ์ธ ์ƒํ’ˆ ํŒ๋งค ID, ์ƒํ’ˆ ID, ํŒ๋งค๋Ÿ‰, ํŒ๋งค์ผ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

Column name Type Nullable
OFFLINE_SALE_ID INTEGER FALSE
PRODUCT_ID INTEGER FALSE
SALES_AMOUNT INTEGER FALSE
SALES_DATE DATE FALSE

๋™์ผํ•œ ๋‚ ์งœ, ์ƒํ’ˆ ID ์กฐํ•ฉ์— ๋Œ€ํ•ด์„œ๋Š” ํ•˜๋‚˜์˜ ํŒ๋งค ๋ฐ์ดํ„ฐ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.


 

๋ฌธ์ œ

PRODUCT ํ…Œ์ด๋ธ”๊ณผ OFFLINE_SALE ํ…Œ์ด๋ธ”์—์„œ ์ƒํ’ˆ์ฝ”๋“œ ๋ณ„ ๋งค์ถœ์•ก(ํŒ๋งค๊ฐ€ * ํŒ๋งค๋Ÿ‰) ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ๋งค์ถœ์•ก์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์‹œ๊ณ  ๋งค์ถœ์•ก์ด ๊ฐ™๋‹ค๋ฉด ์ƒํ’ˆ์ฝ”๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.


โ–  ํ’€์ด

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

PRODUCT ํ…Œ์ด๋ธ”๊ณผ OFFLINE_SALE ํ…Œ์ด๋ธ”์—์„œ ์ƒํ’ˆ์ฝ”๋“œ ๋ณ„ ๋งค์ถœ์•ก(ํŒ๋งค๊ฐ€ * ํŒ๋งค๋Ÿ‰) ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅ 

> ํ…Œ์ด๋ธ” ์กฐ์ธ, GROUP BY, ์ปฌ๋Ÿผ๊ณผ ์ปฌ๋Ÿผ ์ง‘๊ณ„ ํ•จ์ˆ˜ 

๊ฒฐ๊ณผ๋Š” ๋งค์ถœ์•ก์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ                          > ์ •๋ ฌ์กฐ๊ฑด1

๋งค์ถœ์•ก์ด ๊ฐ™๋‹ค๋ฉด ์ƒํ’ˆ์ฝ”๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ     > ์ •๋ ฌ์กฐ๊ฑด2

 

โ— 1์ฐจ ํ’€์ด

  1  ๋‹จ๊ณ„) ํ…Œ์ด๋ธ” ๊ฐ„ inner join , ๊ณตํ†ต ์ปฌ๋Ÿผ์€ PRODUCT_ID

1-1๋‹จ๊ณ„) ํ•„์š”ํ•œ ์ปฌ๋Ÿผ์„ ์ถ”๋ ค์„œ WITH์ ˆ๋กœ ์ž„์‹œํ…Œ์ด๋ธ” 'T1' ์ƒ์„ฑ.

 < ์ž„์‹œํ…Œ์ด๋ธ” T1 ์ปฌ๋Ÿผ ์„ค๋ช… >

์ปฌ๋Ÿผ ๊ฐ’
PRDOUCT_CODE ์ƒํ’ˆ์ฝ”๋“œ
SUM(SALES_AMOUNT)
AS SALES_AMOUNT
์ƒํ’ˆ ์ฝ”๋“œ๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•ด์„œ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•œ
SALES_AMOUNT ๋ผ๋Š” ๋งค์ถœ์•ก ์ปฌ๋Ÿผ ์ƒ์„ฑ
PD. PRICE
AS PRICE
์ƒํ’ˆ ๊ฐ€๊ฒฉ

    

2๋‹จ๊ณ„) ์ž„์‹œ ํ…Œ์ด๋ธ” T1์—์„œ SALES_AMOUNT x PRICE๋กœ ์ƒํ’ˆ์ฝ”๋“œ๋ณ„ ๋งค์ถœ์•ก ๊ณ„์‚ฐํ•ด์„œ

ORDER BY ์— ์œ„์˜ ์ •๋ ฌ์กฐ๊ฑด1, 2 ์ ์šฉํ•ด์„œ ์ถœ๋ ฅ

WITH T1 AS 
    (SELECT PD.PRODUCT_CODE AS PRODUCT_CODE,
           SUM(SALES_AMOUNT) AS SALES_AMOUNT,
           PD. PRICE AS PRICE
     FROM PRODUCT PD
     INNER JOIN OFFLINE_SALE OS 
     ON PD.PRODUCT_ID = OS.PRODUCT_ID
     GROUP BY PD.PRODUCT_CODE)

SELECT T1.PRODUCT_CODE AS PRODUCT_CODE, 
T1.SALES_AMOUNT * T1.PRICE AS SALES
 FROM T1
 ORDER BY 2 DESC, 1;

1์ฐจ ํ’€์ด ํšŒ๊ณ )

์ฝ”๋“œ ๋Œ์•„๊ฐ€๋Š” ์‹œ๊ฐ„์„ ์ค„์—ฌ๋ณด๋ ค๊ณ  ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ–ˆ๋Š”๋ฐ ์ƒ์„ฑํ•˜๊ณ  ์›ํ•˜๋Š” ์ƒํ’ˆ์ฝ”๋“œ๋ณ„ ๋งค์ถœ์•ก ์ถ”์ถœํ•˜๋‹ค๋ณด๋‹ˆ SELECT ์ ˆ์—์„œ ๊ณ„์‚ฐ ์ฟผ๋ฆฌ๋ฅผ ๋„ฃ์–ด์„œ ์ฟผ๋ฆฌ ์ถœ๋ ฅ์— ์—ฌ์ „ํžˆ ์‹œ๊ฐ„์ด ๋” ์†Œ์š”๋  ๊ฒƒ ๊ฐ™๋‹ค.. ๐Ÿ˜จ ์ถœ๋ ฅ์— ์ข€๋” ๊น”๋”ํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ 2์ฐจ ํ’€์ด๋กœ ๋‹ค์‹œ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.