扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
select中嵌套case为何不能用order by
具体代码为:
select bc2.nomb,
(case when getdate(bc2.putdate,bc2.matdate) ='01' then
(select ri.TeamRate
from rate_info ri
where ri.rateyear>= replace(bc2.putoutdate,'/','-')
and ri.teamtime ='1'
and rownum=1
order by ri.rateyear asc)
else 0 end
) businessrate
from bc2,
lc2
where bc2.nomb = lc2.nomb
and bc2.nomb not in(select bd2.no2 from bd2)
and bc2.putdate is not null
and bc2.matdate is not null;
而下面语句确可以正确执行:select ri.TeamRate
from rate_info ri
where ri.rateyear>= replace(bc2.putoutdate,'/','-')
and ri.teamtime ='1'
and rownum=1
order by ri.rateyear asc
我子查询的主要目的是查找大于某个日期的最大利率
还有没有什么好的方法可以改进这个子查询,能达到我要求的目标。
应当是排序之后的最小值。我的case里还有许多其它情况,只是举一个例子。按日期排序,然后取与最小日期对应的利率。
用上GROUP BY 应该 就可以了!
select bc2.nomb,
(case when getdate(bc2.putdate,bc2.matdate) ='01' then
(select ri.TeamRate
from rate_info ri
where ri.rateyear>= replace(bc2.putoutdate,'/','-')
and ri.teamtime ='1'
and rownum=1
order by ri.rateyear asc group by ri.rateyear )
else 0 end
) businessrate
from bc2,
lc2
where bc2.nomb = lc2.nomb
and bc2.nomb not in(select bd2.no2 from bd2)
and bc2.putdate is not null
and bc2.matdate is not null;
select ri.TeamRate
from rate_info ri
where ri.rateyear>= replace(bc2.putoutdate,'/','-')
and ri.teamtime ='1'
and rownum=1
order by ri.rateyear asc
此句查询得到的不是最大值吧
select teamrate
from(
select ri.TeamRate
from rate_info ri
where ri.rateyear>= replace(bc2.putoutdate,'/','-')
and ri.teamtime ='1'
order by ri.rateyear asc
)
where rownum=1 这样才是对的
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者