+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| user_id | int |
| quantity | int |
+-------------+-------+
sale_id contains unique values.
product_id is a foreign key (reference column) to Product table.
Each row of this table shows the ID of the product and the quantity purchased by a user.
Table: Product
+-------------+------+
| Column Name | Type |
+-------------+------+
| product_id | int |
| price | int |
+-------------+------+
product_id contains unique values.
Each row of this table indicates the price of each product.
Write a solution that reports for each user the product id on which the user spent the most money. In case the same user spent the most money on two or more products, report all of them.
WITH user_product_spending AS (
SELECT s.user_id, s.product_id, SUM(s.quantity * p.price) AS spending
FROM Sales s
JOIN Product p ON s.product_id = p.product_id
GROUPBY s.user_id, s.product_id
)
SELECT user_id, product_id
FROM user_product_spending ups
WHERE spending = (
SELECTMAX(spending) FROM user_product_spending WHERE user_id = ups.user_id
);
1
2
3
4
5
6
7
8
9
10
11
WITH user_product_spending AS (
SELECT s.user_id, s.product_id, SUM(s.quantity * p.price) AS spending
FROM Sales s
JOIN Product p ON s.product_id = p.product_id
GROUPBY s.user_id, s.product_id
)
SELECT user_id, product_id
FROM user_product_spending ups
WHERE spending = (
SELECTMAX(spending) FROM user_product_spending WHERE user_id = ups.user_id
);
1
2
3
4
5
6
7
8
9
# Assume Sales and Product are pandas DataFramesimport pandas as pd
defproduct_sales_analysis_iv(Sales: pd.DataFrame, Product: pd.DataFrame) -> pd.DataFrame:
merged = Sales.merge(Product, on='product_id')
merged['spending'] = merged['quantity'] * merged['price']
user_prod = merged.groupby(['user_id', 'product_id'])['spending'].sum().reset_index()
max_spending = user_prod.groupby('user_id')['spending'].transform('max')
result = user_prod[user_prod['spending'] == max_spending][['user_id', 'product_id']]
return result