๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

leetcode > 1484. Group Sold Products By The Date ๋ณธ๋ฌธ

SQL

leetcode > 1484. Group Sold Products By The Date

jhj.sharon 2023. 2. 6. 19:11
๋ฐ˜์‘ํ˜•

โœจ ๋ฌธ์ œ: 1484. Group Sold Products By The Date

https://leetcode.com/problems/group-sold-products-by-the-date/description/?envType=study-plan&id=sql-i

 

๐Ÿ’ป์ฝ”๋“œ

SELECT TO_CHAR(sell_date, 'YYYY-MM-DD') AS sell_date, 
       COUNT(DISTINCT product) as num_sold, 
       LISTAGG( product,',') WITHIN GROUP(ORDER BY product) AS products
FROM (SELECT DISTINCT sell_date, product FROM Activities)
GROUP BY sell_date

 

๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์ด ๋ฐฉ๋ฒ•

  • ํ•ด๋‹น ๋‚ ์งœ์— ํŒ”๋ฆฐ product์˜ ๊ฐœ์ˆ˜, ํ’ˆ๋ชฉ๋ช…์„ ์•Œ์•„๋‚ด์•ผ ํ•œ๋‹ค.
  • ' the number of different products sold ' ๋ฌธ์ œ ์ง€์‹œ์‚ฌํ•ญ์—์„œ ํ•ด๋‹น ๋‚ ์งœ์— ํŒ”๋ฆฐ ๋ฌผํ’ˆ์„ ํ’ˆ๋ชฉ๋ณ„๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์„ธ๋ผ๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— COUNT(DISTINCT product)๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํ•จ์ˆ˜๋Š” MySQL์—์„œ๋Š” group_concat, Oracle์—์„œ๋Š” LISTAGG๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • LISTAGG (์ปฌ๋Ÿผ๋ช…, ๊ตฌ๋ถ„์ž)์˜ ํ˜•ํƒœ๋กœ ์“ฐ๋Š”๋ฐ ๋’ค์— WITHIN GROUP(ORDER BY)๊ฐ€ ํ•ญ์ƒ ๋”ฐ๋ผ์™€์•ผํ•œ๋‹ค. ๋งŒ์•ฝ ๊ตฌ๋ถ„์ž๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ '-' ๊ฐ€ ๋””ํดํŠธ๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค.
  • SELL_DATE๊ฐ€ ์ค‘๋ณต ๋ฐ์ดํ„ฐ๊ฐ’์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— FROM ์ ˆ์—์„œ SELL_DATE๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ผ๋‹ค.

๐Ÿ”” ๋Š๋‚€์ 

  • ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ํ˜ผ์ž ํ’€์ง€ ๋ชปํ•˜๊ณ  ๊ตฌ๊ธ€์— ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„๋ดค๋‹ค. ๋”๋””์ง€๋งŒ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๊ทธ๋•Œ๊ทธ๋•Œ ํ•จ์ˆ˜๋ฅผ ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค.
  • leetcode์—์„œ๋Š” ์ปฌ๋Ÿผ๋ช…์˜ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ accepted์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. ์ž๊พธ ์ด๋ถ€๋ถ„์„ ๊ฐ„๊ณผํ•œ๋‹ค. ์‹ ๊ฒฝ์“ฐ๊ธฐ!
๋ฐ˜์‘ํ˜•

'SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

leetcode > 1141. User Activity for the Past 30 Days I  (0) 2023.02.06
leetcode > 1527. Patients With a Condition  (0) 2023.02.06
leetcode > 1667. Fix Names in a Table  (0) 2023.02.06
leetcode > 196. Delete Duplicate Emails  (0) 2023.02.06
leetcode > 1873. Calculate Special Bonus  (0) 2023.02.05
Comments