返回介绍

solution / 1500-1599 / 1543.Fix Product Name Format / README_EN

发布于 2024-06-17 01:03:18 字数 2904 浏览 0 评论 0 收藏 0

1543. Fix Product Name Format

中文文档

Description

Table: Sales

+--------------+---------+
| Column Name  | Type  |
+--------------+---------+
| sale_id    | int   |
| product_name | varchar |
| sale_date  | date  |
+--------------+---------+
sale_id is the column with unique values 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 a solution 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 result format is in the following example.

 

Example 1:

Input: 
Sales table:
+---------+--------------+------------+
| 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 |
+---------+--------------+------------+
Output: 
+--------------+-----------+-------+
| product_name | sale_date | total |
+--------------+-----------+-------+
| lckeychain   | 2000-02   | 2   |
| lcphone    | 2000-01   | 2   |
| lcphone    | 2000-02   | 1   |
| matryoshka   | 2000-03   | 1   |
+--------------+-----------+-------+
Explanation: 
In January, 2 LcPhones were sold. Please note that the product names are not case sensitive and may contain spaces.
In February, 2 LCKeychains and 1 LCPhone were sold.
In March, one matryoshka was sold.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  t AS (
    SELECT
      LOWER(TRIM(product_name)) AS product_name,
      DATE_FORMAT(sale_date, '%Y-%m') AS sale_date
    FROM Sales
  )
SELECT product_name, sale_date, COUNT(1) AS total
FROM t
GROUP BY 1, 2
ORDER BY 1, 2;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文