如何筛选连续三年增长率大于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 )
;

阅读 2.7k
2 个回答

其实你的 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

大概是这样子

以上。

因为不能用 and。你想一想,有没有符合 report_date='2017'report_date='2018' 的数据。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题