๐ŸณDev/LeetCode

LeetCode : 1484. Group Sold Products By The Date

fortune.00 2023. 5. 13. 23:30

๋ฌธ์ œ

Write an SQL query to find for each date the number of different products sold and their names.

The sold products names for each date should be sorted lexicographically.

Return the result table ordered by sell_date.

The query result format is in the following example.

 

 

ํ’€์ด

๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” ์š”์†Œ์™€ ์กฐ๊ฑด

  • ๋‚ ์งœ : ์ค‘๋ณต ์—†์ด ์˜ค๋ฆ„์ฐจ์ˆœ
  • ๋‚ ์งœ์— ๋”ฐ๋ฅธ ๊ฐ ์ œํ’ˆ์˜ ์ˆ˜
  • ์ œํ’ˆ์˜ ์ด๋ฆ„ : ์ค‘๋ณต ์—†์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌ

  1. ๋‚ ์งœ์— ๋”ฐ๋ผ ๋ฌถ๊ณ , ๋‚ ์งœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์กฐํšŒํ•˜๊ธฐ : group by sell_date, order by sell_date
  2. ์ค‘๋ณต์ด ์—†์ด ์ œํ’ˆ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ : count(distinct product)
  3. group_concat : ํŠน์ • ์—ด์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ํ•˜๋‚˜์˜ ๊ฐ€๋กœ์—ด๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•
    • separator๋กœ ๊ตฌ๋ถ„์ž๋ฅผ ํ‘œ๊ธฐ ๊ฐ€๋Šฅ : separator ','
    • ์ •๋ ฌ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ : order by product
    • ์ค‘๋ณต๊ฐ’ ์ œ๊ฑฐ ๊ฐ€๋Šฅ : distinct product