1070. Product Sales Analysis III 难度:中等

本文介绍了一种SQL查询技巧,用于从销售表中选取每个产品的首次销售年份、销量及价格,通过子查询与联表操作实现精确的数据筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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_idproduct_idyearquantityprice
11002008105000
21002009125000
72002011159000

Product table:

product_idproduct_name
100Nokia
200Apple
300Samsung

Result table:

product_idfirst_yearquantityprice
1002008105000
2002011159000

来源:力扣(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值