1070. Product Sales Analysis III 难度:中等

1、题目描述

Write an SQL query that selects the product id, year, quantity, and price for the first year of every product sold.
The query result format is in the following example:
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

Result table:

product_id first_year quantity price
100 2008 10 5000
200 2011 15 9000

来源:力扣(LeetCode)

2、解题思路

Product table没什么关系
1# 首先增加子表,查询product_id最早出售年份select product_id,min(`year`) as min1 from sales group by product_id
2# 然后2表联查

3、提交记录

select s.product_id,`year` as first_year, quantity,price
from sales s,

(select product_id,min(`year`) as min1
from sales 
group by product_id) gro

where s.product_id=gro.product_id and s.`year`=gro.min1

1340ms

发布了90 篇原创文章 · 获赞 3 · 访问量 4938

猜你喜欢

转载自blog.csdn.net/weixin_43329319/article/details/97615336