Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54cf37f

Browse files
authored
Added task 1484.
1 parent 719a542 commit 54cf37f

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
1484\. Group Sold Products By The Date
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table `Activities`:
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| sell_date | date |
13+
| product | varchar |
14+
+-------------+---------+
15+
There is no primary key for this table, it may contain duplicates.
16+
Each row of this table contains the product name and the date it was sold in a market.
17+
18+
Write an SQL query to find for each date the number of different products sold and their names.
19+
20+
The sold products names for each date should be sorted lexicographically.
21+
22+
Return the result table ordered by `sell_date`.
23+
24+
The query result format is in the following example.
25+
26+
**Example 1:**
27+
28+
**Input:**
29+
30+
Activities table:
31+
+------------+------------+
32+
| sell_date | product |
33+
+------------+------------+
34+
| 2020-05-30 | Headphone |
35+
| 2020-06-01 | Pencil |
36+
| 2020-06-02 | Mask |
37+
| 2020-05-30 | Basketball |
38+
| 2020-06-01 | Bible |
39+
| 2020-06-02 | Mask |
40+
| 2020-05-30 | T-Shirt |
41+
+------------+------------+
42+
43+
**Output:**
44+
45+
+------------+----------+------------------------------+
46+
| sell_date | num_sold | products |
47+
+------------+----------+------------------------------+
48+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
49+
| 2020-06-01 | 2 | Bible,Pencil |
50+
| 2020-06-02 | 1 | Mask |
51+
+------------+----------+------------------------------+
52+
53+
**Explanation:**
54+
55+
For 2020-05-30, Sold items were (Headphone, Basketball, T-shirt), we sort them lexicographically and separate them by a comma.
56+
57+
For 2020-06-01, Sold items were (Pencil, Bible), we sort them lexicographically and separate them by a comma.
58+
59+
For 2020-06-02, the Sold item is (Mask), we just return it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2022_04_06_Time_395_ms_(77.20%)_Space_0B_(100.00%)
3+
SELECT sell_date, COUNT(DISTINCT(product)) as num_sold, GROUP_CONCAT(DISTINCT(product) ORDER BY product) as products
4+
FROM Activities
5+
GROUP BY sell_date

0 commit comments

Comments
 (0)