04/16 SQL 문제풀이 - 1484. Group Sold Products By The Date (Leetcode) (⭐⭐⭐)

Data Architect / Engineer·2024년 4월 16일
1

1일_1SQL

목록 보기
57/63
post-thumbnail

문제

  • LeetCode SQL 문제
  • 1484. Group Sold Products By The Date / Easy
  • 문제 내용 : [링크]


내가 작성한 Query

select sell_date
    , count(distinct product) as num_sold
    , group_concat(distinct product order by product separator ',') as products
from activities
group by sell_date
  • sell_date 별 집계 데이터를 구해야 하므로, sell_date 기준으로 GROUP BY 해준다.

  • count(distinct product)를 통해 num_sold 즉, 총 판매 제품 수를 구해준다. (중복 제외)

  • GROUP_CONCAT을 통해, 판매된 제품의 제품명들을 ','로 구분하여 알파벳 순서로 출력해준다.


⭐⭐⭐
문자열에 GROUP BY의 효과를 낼 수 있는
GROUP_CONCAT(DISTINCT 컬럼명 ORDER BY 컬럼명 SEPERATOR 구분자) 구문을 알아두자!

profile
질문은 계속돼 아오에

0개의 댓글