楼主: Myron2017
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖子

🔗
 楼主| Myron2017 昨天 09:51 | 只看该作者
全局:
1070. Product Sales Analysis III
Medium
Topics
conpanies icon
Companies
SQL Schema
Pandas Schema
Table: Sales

+-------------+-------+
| Column Name | Type  |
+-------------+-------+
| sale_id     | int   |
| product_id  | int   |
| year        | int   |
| quantity    | int   |
| price       | int   |
+-------------+-------+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
Each row records a sale of a product in a given year.
A product may have multiple sales entries in the same year.
Note that the per-unit price.

Write a solution to find all sales that occurred in the first year each product was sold.

For each product_id, identify the earliest year it appears in the Sales table.

Return all sales entries for that product in that year.

Return a table with the following columns: product_id, first_year, quantity, and price.
Return the result in any order.



Example 1:

Input:
Sales table:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1       | 100        | 2008 | 10       | 5000  |
| 2       | 100        | 2009 | 12       | 5000  |
| 7       | 200        | 2011 | 15       | 9000  |
+---------+------------+------+----------+-------+

Output:
+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+
| 100        | 2008       | 10       | 5000  |
| 200        | 2011       | 15       | 9000  |
+------------+------------+----------+-------+

理解解题步骤

第一步:找出每个产品最早销售的年份。

SELECT
    product_id,
    MIN(year) AS first_year
FROM Sales
GROUP BY product_id;

得到中间结果:

product_id

       

first_year




100

       

2008




200

       

2011

这里的 GROUP BY product_id 表示每个产品单独分组,MIN(year) 找出每组最小的年份。

第二步:把中间结果与原表连接。

ON s.product_id = f.product_id
AND s.year = f.first_year

两个条件都必须满足:

产品 ID 相同。

销售年份等于该产品最早销售的年份。

这样就能找回原表中的 quantity 和 price,而且不会遗漏同一产品在最早年份的其他销售记录。

方法二:使用 MIN() 窗口函数

如果你正在学习窗口函数,也可以这样写:

SELECT
    product_id,
    year AS first_year,
    quantity,
    price
FROM (
    SELECT
        *,
        MIN(year) OVER (
            PARTITION BY product_id
        ) AS first_year
    FROM Sales
) AS s
WHERE year = first_year;

这里:

PARTITION BY product_id:按产品分别计算。

MIN(year) OVER (...):计算每个产品最早的年份,但保留原表的每一行。

WHERE year = first_year:只保留最早年份的销售记录。
  1. SELECT
  2.     s.product_id,
  3.     s.year AS first_year,
  4.     s.quantity,
  5.     s.price
  6. FROM Sales AS s
  7. JOIN (
  8.     SELECT
  9.         product_id,
  10.         MIN(year) AS first_year
  11.     FROM Sales
  12.     GROUP BY product_id
  13. ) AS f
  14. ON s.product_id = f.product_id
  15. AND s.year = f.first_year;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表