1+ 1484 . Group Sold Products By The Date
2+ Solved
3+ Easy
4+ Topics
5+ Companies
6+ SQL Schema
7+ Pandas Schema
8+ Table Activities:
9+
10+ + -- -----------+---------+
11+ | Column Name | Type |
12+ + -- -----------+---------+
13+ | sell_date | date |
14+ | product | varchar |
15+ + -- -----------+---------+
16+ There is no primary key (column with unique values ) for this table. It may contain duplicates.
17+ Each row of this table contains the product name and the date it was sold in a market.
18+
19+
20+ Write a solution to find for each date the number of different products sold and their names.
21+
22+ The sold products names for each date should be sorted lexicographically.
23+
24+ Return the result table ordered by sell_date.
25+
26+ The result format is in the following example.
27+
28+
29+
30+ Example 1 :
31+
32+ Input:
33+ Activities table:
34+ + -- ----------+------------+
35+ | sell_date | product |
36+ + -- ----------+------------+
37+ | 2020 - 05 - 30 | Headphone |
38+ | 2020 - 06 - 01 | Pencil |
39+ | 2020 - 06 - 02 | Mask |
40+ | 2020 - 05 - 30 | Basketball |
41+ | 2020 - 06 - 01 | Bible |
42+ | 2020 - 06 - 02 | Mask |
43+ | 2020 - 05 - 30 | T- Shirt |
44+ + -- ----------+------------+
45+ Output:
46+ + -- ----------+----------+------------------------------+
47+ | sell_date | num_sold | products |
48+ + -- ----------+----------+------------------------------+
49+ | 2020 - 05 - 30 | 3 | Basketball,Headphone,T- shirt |
50+ | 2020 - 06 - 01 | 2 | Bible,Pencil |
51+ | 2020 - 06 - 02 | 1 | Mask |
52+ + -- ----------+----------+------------------------------+
53+ Explanation:
54+ For 2020 - 05 - 30 , Sold items were (Headphone, Basketball, T- shirt), we sort them lexicographically and separate them by a comma.
55+ For 2020 - 06 - 01 , Sold items were (Pencil, Bible), we sort them lexicographically and separate them by a comma.
56+ For 2020 - 06 - 02 , the Sold item is (Mask), we just return it.
57+
58+
59+
60+ select sell_date, count ( DISTINCT product ) as num_sold ,
61+
62+ GROUP_CONCAT( DISTINCT product order by product ASC separator ' ,' ) as products
63+
64+ FROM Activities GROUP BY sell_date order by sell_date ASC ;
0 commit comments