EasyNo. 1069LeetCode Premium
Product Sales Analysis II — LeetCode 1069 Python Solution
- Topics
- Database
- Reading time
- 2 min
- Source
- leetcode.com
The problem
For every product, report its id and the total quantity sold across all years, in any order. The Sales table has one row per sale: sale_id (int), product_id (int), year (int), quantity (int) and price (int, the price per unit), with the pair (sale_id, year) as the primary key. The Product table maps product_id (int, the primary key) to product_name (varchar).
Example
Sales table: | sale_id | product_id | year | quantity | price | | ------- | ---------- | ---- | -------- | ----- | | 11 | 300 | 2019 | 25 | 1200 | | 12 | 300 | 2020 | 30 | 1150 | | 13 | 400 | 2020 | 8 | 4300 | Product table: | product_id | product_name | | ---------- | ------------ | | 300 | Router | | 400 | Laptop | Result: | product_id | total_quantity | | ---------- | -------------- | | 300 | 55 | | 400 | 8 | Product 300 sold 25 units in 2019 and 30 in 2020 for a total of 55, and product 400 sold 8 units in a single year.
Python solution
import pandas as pd
def sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:
res = sales.groupby('product_id', as_index=False)['quantity'].sum()
res.columns = ['product_id', 'total_quantity']
return resComplexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1068 | Product Sales Analysis I | Easy |
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
Frequently asked questions
- How hard is LeetCode 1069. Product Sales Analysis II?
LeetCode 1069. Product Sales Analysis II is rated Easy on LeetCode.
- What topics does LeetCode 1069. Product Sales Analysis II cover?
LeetCode 1069. Product Sales Analysis II is tagged Database on LeetCode.
- Is LeetCode 1069. Product Sales Analysis II a premium problem?
Yes. LeetCode 1069. Product Sales Analysis II is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.