Group Sold Products By The Date
EasyLeetCodeSQL: CASE, String & Date Functions
Hint and exampleShowHide
Group by the date, count the distinct values and join them into one sorted string with GROUP_CONCAT in MySQL or STRING_AGG in PostgreSQL.
Example: orders(order_day, dish)
order_day | dish
2024-05-01 | Tea
2024-05-01 | Bun
2024-05-01 | Tea
2024-05-02 | Dosa
SELECT order_day,
COUNT(DISTINCT dish) AS dishes,
GROUP_CONCAT(DISTINCT dish ORDER BY dish) AS menu
FROM orders
GROUP BY order_day;
-- 2024-05-01 | 2 | Bun,Tea
-- 2024-05-02 | 1 | Dosa
More in SQL: CASE, String & Date Functions
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Triangle Judgement | LeetCode | Easy | Solve Triangle Judgement on LeetCode (opens in a new tab) | |||
| 2 | Fix Names in a Table | LeetCode | Easy | Solve Fix Names in a Table on LeetCode (opens in a new tab) | |||
| 3 | Rising Temperature | LeetCode | Easy | Solve Rising Temperature on LeetCode (opens in a new tab) | |||
| 4 | Group Sold Products By The Date | LeetCode | Easy | Solve Group Sold Products By The Date on LeetCode (opens in a new tab) | |||
| 5 | Monthly Transactions I | LeetCode | Medium | Solve Monthly Transactions I on LeetCode (opens in a new tab) | |||
| 6 | Confirmation Rate | LeetCode | Medium | Solve Confirmation Rate on LeetCode (opens in a new tab) |