Growth ๐ŸŒณ/Practice ๐Ÿ’ป

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

์ธ” 2023. 1. 12. 12:55

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์€ Growth/Practice ๊ฒŒ์‹œ๊ธ€ (https://sy038680.tistory.com/235) ๋ฌธ์ œ์— ๋Œ€ํ•œ 2์ฐจ ํ’€์ด์ž…๋‹ˆ๋‹ค.

 


๊ธฐ์กด ํ’€์ด๋Š” WITH์ ˆ์„ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ WITH์ ˆ์„ ์ด์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ๋ฌธ์ œ์˜€๋‹ค.

ํ’€์ด์ค‘ LEFT JOIN์„ ์‚ฌ์šฉํ–ˆ๋”๋‹ˆ PRODUCT ํ…Œ์ด๋ธ”์—์„œ ์˜คํ”„๋ผ์ธ ํŒ๋งค์ •๋ณด๊ฐ€ ์—†๋˜ ์ƒํ’ˆ๋“ค์— ๋Œ€ํ•ด ์ปฌ๋Ÿผ๊ฐ’ Null๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

 ์ด๋ฅผ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•ด INNER JOIN์„ ์‚ฌ์šฉํ•˜๊ณ 

 

 ๋งค์ถœ์•ก = ( ํŒ๋งค๊ฐ€ x  ํŒ๋งค๋Ÿ‰  )

                         ๊ฐ€๊ฒฉ x ์ด ํŒ๋งค์ˆ˜๋Ÿ‰ 

 

๋ฅผ PRODUCT_CODE๋ณ„๋กœ GROUPINGํ•˜๊ณ  ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

SELECT PD.PRODUCT_CODE, 
       PD.PRICE * SUM(OS.SALES_AMOUNT) AS SALES
 FROM PRODUCT PD
 INNER JOIN OFFLINE_SALE OS
 ON PD.PRODUCT_ID = OS.PRODUCT_ID
 GROUP BY 1
 ORDER BY 2 DESC, 1;

 

<๊ธฐ์กดํ’€์ด>

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;