项目场景:
根据同一卷号 27AA261A00 获取最晚生产时间 20220907144200
问题描述
一开始使用limit过滤最晚的时间,结果报错 ORA-00933: SQL 命令未正确结束
limit过滤出最晚时间语句
select t.mat_no,t.out_stock_time
from RH_CPCK t
where mat_no='27AA261A00'
order by out_stock_time desc limit 1;
原因分析:
提示:因为Oracle不支持limit 函数。所以会报错
解决方案:
方法一:使用 rownum 函数代替limit
select t.mat_no,t.out_stock_time
from RH_CPCK t
where mat_no='27AA261A00' and rownum <= 1
order by out_stock_time desc ;
方法二:使用 max 函数
select mat_no,max(out_stock_time) as out_stock_time
from RH_CPCK
where mat_no='27AA261A00'
group by mat_no ;
方法三:
select DISTINCT t.mat_no ,
LAST_VALUE (t.out_stock_time)
OVER (PARTITION BY t.mat_no ORDER BY t.out_stock_time ROWS BETWEEN unbounded preceding AND unbounded following) AS last_RZHI
from RH_CPCK t
where mat_no = '27A0142A00' ;
以上几种方法都可以过滤出组内最晚时间的记录