코드카타/SQL, Pandas

Product Sales Analysis 1

susinlee 2024. 12. 20. 08:57

[문제] https://leetcode.com/problems/product-sales-analysis-i/description/

Write a solution to report the product_name, year, and price for each sale_id in the Sales table.

Return the resulting table in any order.

The result format is in the following example.

 

[풀이]

Pandas

import pandas as pd

def sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:
    df = pd.merge(sales, product, on='product_id', how='inner')
    
    return df[['product_name', 'year', 'price']]

 

SQL

# Write your MySQL query statement below
SELECT p.product_name,
       s.year,
       s.price
FROM Sales s
LEFT JOIN Product p
    ON s.product_id = p.product_id