Group Sold Products By The Date — LeetCode 1484 Python Solution
EasyDatabase
- Problem
- #1484
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
SQL
Table Activities: +-------------+---------+ | Column Name | Type | +-------------+---------+ | sell_date | date | | product | varchar | +-------------+---------+ There is no primary key (column with unique values) for this table. It may contain duplicates.Example
SQL
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each row of this table contains the product name and the date it was sold in a market.Python solution
Python
import duckdb
import pandas as pd
# Pass input tables as keyword arguments matching the SQL table names.
def solution(**tables) -> pd.DataFrame:
con = duckdb.connect()
for name, df in tables.items():
con.register(name, df)
return con.execute("""SELECT
sell_date,
COUNT(DISTINCT product) AS num_sold,
GROUP_CONCAT(DISTINCT product) AS products
FROM Activities
GROUP BY sell_date
ORDER BY sell_date;""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1484. Group Sold Products By The Date?
- LeetCode 1484. Group Sold Products By The Date is rated Easy on LeetCode.
- What topics does LeetCode 1484. Group Sold Products By The Date cover?
- LeetCode 1484. Group Sold Products By The Date is tagged Database on LeetCode.