Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

如何筛选连续三年增长率大于10%?

select code,report_date,revenue from growth
where report_date = '2017' and grow >0.1
;

可以获得2017年,该年增长率大于10%的公司名称。

select code,report_date,revenue from growth
where report_date = '2018' and grow >0.1
;

可以获得2018年,该年增长率大于10%的公司名称。

select code,report_date,revenue from growth
where report_date = '2019' and grow >0.1
;

可以获得2019年,该年增长率大于10%的公司名称。

需要获得连续这三年,每年增长率都大于10%的公司名称。

为何不可以写

select code,report_date,revenue from growth
where (report_date = '2017' and grow >0.1 )
and (report_date = '2018' and grow >0.1 )
and (report_date = '2019' and grow >0.1 )
;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

其实你的 where 语句是 report_date = '2017' and report_date = '2018' and report_date = '2019' and grow >0.1 。

这当然不会查询出结果。

想到查出连续三年,应该使用主键去关联同一张表三次, reportdate 作为查询条件分别查询报告日期。

比如 id 是这张表的主键,你可以这么写:

select a.code,a.report_date,a.revenue from growth a
inner join growth b on a.id=b.id
inner join growth c on c.id=d.id
where a.report_date='2017' and a.grow>0.1
and b.report_date='2018' and b.grow>0.1
and c.report_date='2019' and c.grow>0.1

大概是这样子

以上。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...