Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์šฉ์ž์™€ ์ด ๊ฑฐ๋ž˜๊ธˆ์•ก ์กฐํšŒํ•˜๊ธฐ

์ธ” 2023. 4. 7. 16:09

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

       ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต,   

      https://school.programmers.co.kr/learn/challenges?page=1&languages=mysql%2Coracle์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

      · ๋ณธ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/164668


โ–  ๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

 

๋‹ค์Œ์€ ์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ฒŒ์‹œํŒ ์ •๋ณด๋ฅผ ๋‹ด์€ USED_GOODS_BOARD ํ…Œ์ด๋ธ”๊ณผ ์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ฒŒ์‹œํŒ ์ฒจ๋ถ€ํŒŒ์ผ ์ •๋ณด(์ž์ฒด์ˆ˜์ •) ์ž‘์„ฑ์ž ์ •๋ณด๋ฅผ ๋‹ด์€ USED_GOODS_FILE ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. 

 USED_GOODS_BOARD ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ BOARD_ID, WRITER_ID, TITLE, CONTENTS, PRICE, CREATED_DATE, STATUS, VIEWS๋Š” ๊ฒŒ์‹œ๊ธ€ ID, ์ž‘์„ฑ์ž ID, ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ, ๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ, ๊ฐ€๊ฒฉ, ์ž‘์„ฑ์ผ, ๊ฑฐ๋ž˜์ƒํƒœ, ์กฐํšŒ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Column name Type Nullable
BOARD_ID VARCHAR(5) FALSE
WRITER_ID VARCHAR(50) FALSE
TITLE VARCHAR(100) FALSE
CONTENTS VARCHAR(1000) FALSE
PRICE NUMBER FALSE
CREATED_DATE DATE FALSE
STATUS VARCHAR(10) FALSE
VIEWS NUMBER FALSE

 

USED_GOODS_USER ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ USER_IDNICKNAMECITYSTREET_ADDRESS1STREET_ADDRESS2TLNO๋Š” ๊ฐ๊ฐ ํšŒ์› ID, ๋‹‰๋„ค์ž„, ์‹œ, ๋„๋กœ๋ช… ์ฃผ์†Œ, ์ƒ์„ธ ์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Column name Type Nullable
USER_ID VARCHAR(50) FALSE
NICKANME VARCHAR(100) FALSE
CITY VARCHAR(100) FALSE
STREET_ADDRESS1 VARCHAR(100) FALSE
STREET_ADDRESS2 VARCHAR(100) TRUE
TLNO VARCHAR(20) FALSE

 


๋ฌธ์ œ

USED_GOODS_BOARD์™€ USED_GOODS_USER ํ…Œ์ด๋ธ”์—์„œ ์™„๋ฃŒ๋œ ์ค‘๊ณ  ๊ฑฐ๋ž˜์˜ ์ด๊ธˆ์•ก์ด 70๋งŒ ์› ์ด์ƒ์ธ ์‚ฌ๋žŒ์˜ ํšŒ์› ID, ๋‹‰๋„ค์ž„, ์ด๊ฑฐ๋ž˜๊ธˆ์•ก์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๊ฒฐ๊ณผ๋Š” ์ด๊ฑฐ๋ž˜๊ธˆ์•ก์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.


โ–  ํ’€์ด

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

 

> ํ…Œ์ด๋ธ” ๊ฐ„ ๊ณตํ†ต ์ปฌ๋Ÿผ์ธ  USED_GOODS_BOARD์˜ WRITER_ID์™€ USED_GOODS_USER์˜ USED_ID๋กœ ์กฐ์ธ

   (๊ฑฐ๋ž˜ ๊ฒŒ์‹œํŒ๊ณผ ์‚ฌ์šฉ์ž ์ •๋ณด ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ๊ด€๊ณ„๋Š” N:1)

 

์™„๋ฃŒ๋œ ์ค‘๊ณ  ๊ฑฐ๋ž˜์˜ ์ด๊ธˆ์•ก์ด 70๋งŒ ์› ์ด์ƒ์ธ ์‚ฌ๋žŒ์˜ ํšŒ์› ID, ๋‹‰๋„ค์ž„, ์ด๊ฑฐ๋ž˜๊ธˆ์•ก์„ ์กฐํšŒ

 

> '์™„๋ฃŒ๋œ' → STATUS๊ฐ’์ด 'DONE'

> ํšŒ์› ID๋ณ„๋กœ ์ค‘๊ณ  ๊ฑฐ๋ž˜ ๊ธˆ์•ก์„ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ GROUP BY

 

๊ฒฐ๊ณผ๋Š” ์ด๊ฑฐ๋ž˜๊ธˆ์•ก์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

> ์ •๋ ฌ์กฐ๊ฑด

SELECT T1.WRITER_ID AS USER_ID,
       T2.NICKNAME AS NICKNAME,
       SUM(T1.PRICE) AS TOTAL_SALES
 FROM USED_GOODS_BOARD T1
 LEFT JOIN USED_GOODS_USER T2
 ON T1.WRITER_ID = T2.USER_ID
 
 WHERE T1.STATUS = 'DONE'
 GROUP BY T1.WRITER_ID
 HAVING SUM(T1.PRICE) >= 700000
 ORDER BY 3 ASC;