Fix Product Name Format Problem


Description

LeetCode Problem 1543.

Table: Sales

1
2
3
4
5
6
7
8
9
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| sale_id      | int     |
| product_name | varchar |
| sale_date    | date    |
+--------------+---------+
sale_id is the primary key for this table.
Each row of this table contains the product name and the date it was sold.

Since table Sales was filled manually in the year 2000, product_name may contain leading and/or trailing white spaces, also they are case-insensitive.

Write an SQL query to report

  • product_name in lowercase without leading or trailing white spaces.
  • sale_date in the format (‘YYYY-MM’)
  • total the number of times the product was sold in this month.

Return the result table ordered by product_name in ascending order, in case of a tie order it by sale_date in ascending order.

The query result format is in the following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Sales
+------------+------------------+--------------+
| sale_id    | product_name     | sale_date    |
+------------+------------------+--------------+
| 1          |      LCPHONE     | 2000-01-16   |
| 2          |    LCPhone       | 2000-01-17   |
| 3          |     LcPhOnE      | 2000-02-18   |
| 4          |      LCKeyCHAiN  | 2000-02-19   |
| 5          |   LCKeyChain     | 2000-02-28   |
| 6          | Matryoshka       | 2000-03-31   | 
+------------+------------------+--------------+

Result table:
+--------------+--------------+----------+
| product_name | sale_date    | total    |
+--------------+--------------+----------+
| lcphone      | 2000-01      | 2        |
| lckeychain   | 2000-02      | 2        | 
| lcphone      | 2000-02      | 1        | 
| matryoshka   | 2000-03      | 1        | 
+--------------+--------------+----------+

In January, 2 LcPhones were sold, please note that the product names are not case sensitive and may contain spaces.
In Februery, 2 LCKeychains and 1 LCPhone were sold.
In March, 1 matryoshka was sold.


MySQL Solution

1
2
3
4
5
6
select trim(lower(product_name)) as product_name,
    left(sale_date,7) as sale_date,
    count(sale_id) as total
from Sales
group by 1, 2
order by 1, 2




Related Posts

Bank Account Summary Problem

LeetCode 1555. Our bank records all transactions in the table...

The Most Recent Orders For Each Product Problem

LeetCode 1549. Write an SQL query to find the most...

Fix Product Name Format Problem

LeetCode 1543. Write an SQL query to report product_name in...

Unique Orders And Customers Per Month Problem

LeetCode 1565. Write an SQL query to find the number...

Patients With A Condition Problem

LeetCode 1527. Write an SQL query to report the patient_id,...