点击关注公众号,SQL干货及时获取
后台回复:1024,获取500G视频教程 推荐阅读 成为数据分析师 牛逼,OpenAI新模型 o1 国内直接连! 《SQL145题第2版》正式发布!
计算机成下一个土木?
岳哥看法
题目:最值陷阱
参考答案
create table T20191114
(
shipid varchar(10),
paydate date,
payno int
)
insert into T20191114 values
('1001','2019-11-02',5);
insert into T20191114 values
('1001','2019-11-02',3);
insert into T20191114 values
('1001','2019-11-03',1);
insert into T20191114 values
('1001','2019-11-03',3);
insert into T20191114 values
('1002','2019-11-09',1);
insert into T20191114 values
('1002','2019-11-09',4);
insert into T20191114 values
('1002','2019-11-08',3);
insert into T20191114 values
('1002','2019-11-08',2);
--MySQL 8和SQL Server解法
select a.shipid,b.min_date,min(a.payno) min_payno
from T20191114 a
join
(
select shipid,min(paydate) min_date
from T20191114 group by shipid
) b
on a.shipid=b.shipid
and a.paydate=b.min_date
group by a.shipid,b.min_date