반응형

Table Activities:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each row of this table contains the product name and the date it was sold in a market.
Write a solution 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 result format is in the following example.
Example 1:
Input:
Activities table:
+------------+------------+
| sell_date | product |
+------------+------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
+------------+------------+
Output:
+------------+----------+------------------------------+
| sell_date | num_sold | products |
+------------+----------+------------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
+------------+----------+------------------------------+
Explanation:
For 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by a comma.
For 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by a comma.
For 2020-06-02, the Sold item is (Mask), we just return it.
- 각 날짜별로 중복 없이 판매된 제품 수 (num_sold)
- 제품 이름들을 알파벳순으로 정렬한 문자열 (products)
- 결과는 sell_date 기준 오름차순 정렬
select
sell_date,
count(distinct product) as num_sold,
GROUP_CONCAT(distinct product order by product) as products
from activities
group by sell_date
반응형
'Computer Science > SQL' 카테고리의 다른 글
SQL | HackerRank 삼각형 분류하기: 조건문 CASE를 활용한 다중 조건 처리 (0) | 2025.06.11 |
---|---|
SQL | LeetCode 1327. 특정 월 기준 주문량 분석하기 (0) | 2025.06.10 |
SQL | LeetCode 176. 두 번째 최대값 구하기 (DENSE_RANK) (0) | 2025.06.08 |
SQL | LeetCode 1667. 이름 포맷 통일하기 (CONCAT, UPPER, LOWER) (0) | 2025.06.07 |
SQL | LeetCode 185. DENSE_RANK()로 부서별 Top 3 급여자 구하기 (2) | 2025.06.06 |
반응형

Table Activities:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each row of this table contains the product name and the date it was sold in a market.
Write a solution 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 result format is in the following example.
Example 1:
Input:
Activities table:
+------------+------------+
| sell_date | product |
+------------+------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
+------------+------------+
Output:
+------------+----------+------------------------------+
| sell_date | num_sold | products |
+------------+----------+------------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
+------------+----------+------------------------------+
Explanation:
For 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by a comma.
For 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by a comma.
For 2020-06-02, the Sold item is (Mask), we just return it.
- 각 날짜별로 중복 없이 판매된 제품 수 (num_sold)
- 제품 이름들을 알파벳순으로 정렬한 문자열 (products)
- 결과는 sell_date 기준 오름차순 정렬
select
sell_date,
count(distinct product) as num_sold,
GROUP_CONCAT(distinct product order by product) as products
from activities
group by sell_date
반응형
'Computer Science > SQL' 카테고리의 다른 글
SQL | HackerRank 삼각형 분류하기: 조건문 CASE를 활용한 다중 조건 처리 (0) | 2025.06.11 |
---|---|
SQL | LeetCode 1327. 특정 월 기준 주문량 분석하기 (0) | 2025.06.10 |
SQL | LeetCode 176. 두 번째 최대값 구하기 (DENSE_RANK) (0) | 2025.06.08 |
SQL | LeetCode 1667. 이름 포맷 통일하기 (CONCAT, UPPER, LOWER) (0) | 2025.06.07 |
SQL | LeetCode 185. DENSE_RANK()로 부서별 Top 3 급여자 구하기 (2) | 2025.06.06 |