EasyNo. 1543LeetCode Premium
Fix Product Name Format — LeetCode 1543 Python Solution
- Topics
- Database
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
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.Example
+--------------+---------+
| 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.Python solution
import duckdb
import pandas as pd
def solution(sales: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Sales", sales)
return con.execute("""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;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 1543. Fix Product Name Format?
LeetCode 1543. Fix Product Name Format is rated Easy on LeetCode.
- What topics does LeetCode 1543. Fix Product Name Format cover?
LeetCode 1543. Fix Product Name Format is tagged Database on LeetCode.
- Is LeetCode 1543. Fix Product Name Format a premium problem?
Yes. LeetCode 1543. Fix Product Name Format is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.