🤔 문제 분석
1) Write an SQL query to find for each date the number of different products sold and their names.
2) The sold products names for each date should be sorted lexicographically.
3) Return the result table ordered by 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
ORDER BY sell_date
✍️ check point
문자열을 병합할 때 concat을 사용한다.
그러나 group by에서 concat을 사용하면 해당하는 행 중 하나의 값만 도출된다.
합친 문자열이 필요한 경우에는 group_concat을 사용하면 된다.
또한 정렬하고자 하는 문자열이 있다면,
order by 문자열 DESC 등으로 작성하면 된다.
'코딩테스트 > 리트코드 SQL' 카테고리의 다른 글
[리트코드/SQL] 11. Employees With Missing Information (1965) (0) | 2022.12.16 |
---|---|
[리트코드/SQL] 10. Patients With a Condition (1527) (0) | 2022.12.08 |
[리트코드/SQL] 8. Fix Names in a Table (1667) (0) | 2022.12.08 |
[리트코드/SQL] 7. Delete Duplicate Emails (196) (0) | 2022.12.05 |
[리트코드/SQL] 6. Swap Salary (627) (0) | 2022.12.05 |
댓글