1068. 产品销售分析 I
1068. 产品销售分析 I
题目
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.
product_id is a foreign key (reference column) to Product table.
Each row of this table shows a sale on the product product_id in a certain year.
Note that the price is per unit.
Table: Product
+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | +--------------+---------+
product_id is the primary key (column with unique values) of this table.
Each row of this table indicates the product name of each product.
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.
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 | +---------+------------+------+----------+-------+
Product table:
+------------+--------------+ | product_id | product_name | +------------+--------------+ | 100 | Nokia | | 200 | Apple | | 300 | Samsung | +------------+--------------+
Output:
+--------------+-------+-------+ | product_name | year | price | +--------------+-------+-------+ | Nokia | 2008 | 5000 | | Nokia | 2009 | 5000 | | Apple | 2011 | 9000 | +--------------+-------+-------+
Explanation:
From sale_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008.
From sale_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009.
From sale_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
题目大意
销售表 Sales
:
+-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+
(sale_id, year) 是销售表 Sales 的主键(具有唯一值的列的组合)。
product_id 是关联到产品表 Product 的外键(reference 列)。
该表的每一行显示 product_id 在某一年的销售情况。
注意: price 表示每单位价格。
产品表 Product
:
+--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | +--------------+---------+
product_id 是表的主键(具有唯一值的列)。
该表的每一行表示每种产品的产品名称。
编写解决方案,以获取 Sales
表中所有 sale_id
对应的 product_name
以及该产品的所有 year
和 price
。
返回结果表 无顺序要求 。
结果格式示例如下。
示例 1:
输入:
Sales 表:
+---------+------------+------+----------+-------+ | sale_id | product_id | year | quantity | price | +---------+------------+------+----------+-------+ | 1 | 100 | 2008 | 10 | 5000 | | 2 | 100 | 2009 | 12 | 5000 | | 7 | 200 | 2011 | 15 | 9000 | +---------+------------+------+----------+-------+
Product table:
+------------+--------------+ | product_id | product_name | +------------+--------------+ | 100 | Nokia | | 200 | Apple | | 300 | Samsung | +------------+--------------+
输出:
+--------------+-------+-------+ | product_name | year | price | +--------------+-------+-------+ | Nokia | 2008 | 5000 | | Nokia | 2009 | 5000 | | Apple | 2011 | 9000 | +--------------+-------+-------+
解题思路
返回字段:
使用SELECT
指定要返回的字段:p.product_name
:来自Product
表,表示产品名称。s.year
:来自Sales
表,表示销售年份。s.price
:来自Sales
表,表示销售价格。
表来源与连接:
- 数据来源于两个表:
Product
表和Sales
表。 - 使用
JOIN
将两张表连接,关联条件为p.product_id = s.product_id
,即通过两表的product_id
字段进行匹配。 - 仅保留
Product
和Sales
表中存在匹配记录的数据。
- 数据来源于两个表:
复杂度分析
时间复杂度:
O(n * m)
,其中n
是Product
表的记录数,m
是Sales
表的记录数。JOIN
操作需要O(n * m)
次遍历两张表来匹配product_id
字段。- 如果使用索引优化,通过索引快速匹配
product_id
字段,复杂度可以降低到O(n + m)
。
- 如果使用索引优化,通过索引快速匹配
空间复杂度:
O(k)
,其中k
是返回的结果集大小,结果集的空间需求与匹配记录数成正比。
代码
SELECT p.product_name, s.year, s.price
FROM Product p
JOIN Sales s
ON p.product_id = s.product_id
相关题目
题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
---|---|---|---|---|---|
1069 | 产品销售分析 II 🔒 | 数据库 | 🟢 | 🀄️ 🔗 | |
2324 | 产品销售分析 IV 🔒 | 数据库 | 🟠 | 🀄️ 🔗 | |
2329 | 产品销售分析Ⅴ 🔒 | 数据库 | 🟢 | 🀄️ 🔗 |