코딩일기
SQL 코드카타 124 - Group Sold Products By The Date
서재일
2024. 7. 14. 14:36
https://leetcode.com/problems/group-sold-products-by-the-date/description/
Column Name | Type |
sell_date | date |
product | varchar |
Activities 테이블:
- sell_date : 상품이 판매된 날짜
- product : 상품의 이름
각 날짜별로 판매된 서로 다른 제품의 수와 그 이름을 찾으세요.
SELECT *
FROM Activities
group by sell_date, product
완전히 동일한 데이터를 제거하기 위해서 두 열을 모두 그룹화한다.
select *
from
(SELECT *
FROM Activities
group by sell_date, product) a
group by sell_date
날짜별 상품의 숫자를 계산하기 위해서 날짜별로 그룹화한다.
select
sell_date,
count(product) num_sold
from
(SELECT *
FROM Activities
group by sell_date, product) a
group by sell_date
날짜와 날짜별 상품의 갯수를 출력한다.
select
sell_date,
count(product) num_sold,
group_concat(product order by product separator ',') products
from
(SELECT *
FROM Activities
group by sell_date, product) a
group by sell_date
group_concat을 사용해서 조건에 맞는 priduct들을 결합한다.
priduct를 오름차순 정렬해서 ','로 구분해여 출력한다.