Fix Product Name Format - Problem
Given a Sales table that contains product sales data from the year 2000, you need to clean and format the product names and aggregate sales data by month.
Table: Sales
| Column Name | Type |
|---|---|
| sale_id | int |
| product_name | varchar |
| sale_date | date |
Requirements:
- Clean
product_nameby removing leading/trailing whitespace and converting to lowercase - Format
sale_dateto 'YYYY-MM' format - Count total sales for each product per month
- Order by product_name ascending, then by sale_date ascending
Table Schema
Sales
| Column Name | Type | Description |
|---|---|---|
sale_id
PK
|
int | Unique identifier for each sale |
product_name
|
varchar | Product name (may have whitespace and mixed case) |
sale_date
|
date | Date when the product was sold |
Primary Key: sale_id
Note: Table was filled manually in 2000, so product names may have inconsistent formatting
Input & Output
Example 1 — Basic cleaning and grouping
Input Table:
| sale_id | product_name | sale_date |
|---|---|---|
| 1 | LC Aloe vera | 2000-01-01 |
| 2 | LC Aloe vera | 2000-01-15 |
| 3 | CHAMOMILE | 2000-02-01 |
| 4 | Chamomile | 2000-02-10 |
Output:
| product_name | sale_date | total |
|---|---|---|
| chamomile | 2000-02 | 2 |
| lc aloe vera | 2000-01 | 2 |
💡 Note:
The query cleans product names by removing whitespace and converting to lowercase. ' LC Aloe vera ' and 'LC Aloe vera' both become 'lc aloe vera'. Sales dates are formatted to month format, and sales are counted per product per month.
Example 2 — Single product multiple months
Input Table:
| sale_id | product_name | sale_date |
|---|---|---|
| 1 | Green Tea | 2000-03-01 |
| 2 | GREEN TEA | 2000-03-15 |
| 3 | green tea | 2000-04-01 |
Output:
| product_name | sale_date | total |
|---|---|---|
| green tea | 2000-03 | 2 |
| green tea | 2000-04 | 1 |
💡 Note:
All variations of 'Green Tea' are normalized to 'green tea'. The product appears in two different months (March and April), so we get separate rows for each month with their respective counts.
Constraints
-
1 ≤ sale_id ≤ 1000 -
product_namecontains only alphabetic characters, spaces, and common punctuation - All dates are from the year 2000
-
sale_dateis in valid date format
Visualization
Tap to expand
Understanding the Visualization
1
Raw Data
Messy product names with whitespace
2
Clean & Group
String functions + aggregation
3
Monthly Summary
Clean names with counts
Key Takeaway
🎯 Key Insight: Use string functions with GROUP BY to clean and aggregate messy data in a single query
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code