LeetCode 1484. Group Sold Products By The Date

Description

https://leetcode.com/problems/group-sold-products-by-the-date/

SQL Schema

Table Activities:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| sell_date   | date    |
| product     | varchar |
+-------------+---------+
There is no primary key for this table, it may contains duplicates.
Each row of this table contains the product name and the date it was sold in a market.

Write an SQL query to find for each date, the number of distinct 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 query result format is in the following example.

Activities

Explanation

GROUP_CONCAT with ORDER BY and SEPARATOR. Notice the DISTINCT.

SQL Solution

# Write your MySQL query statement below
select sell_date, count(distinct product) as num_sold, GROUP_CONCAT(distinct product order by product ASC SEPARATOR ',') as products 
from Activities 
group by sell_date 

Leave a Reply

Your email address will not be published. Required fields are marked *