-- 创建临时表
create temporary table t
as select sc.id,sc.item_id,sc.settle_biz_date
from biz_sc sc
where settle_biz_date between '2022-07-01' and '2022-09-19'
and is_settled=true and delflg=0 and pkg_flg in (0,2) and last_qty=1 and item_id=3021900000000000276;
-- 循环排序top
select t3.id,t3.settle_biz_date FROM
(select t1.*,(select count(*)+1 from t t2
where t2.settle_biz_date = t1.settle_biz_date
AND t2.id > t1.id
) top from t t1
) t3 where top <= 10 order by settle_biz_date,id;
-- 删除临时表
drop table if EXISTS t;
SQL-根据时间筛选前十条数据
于 2022-09-20 21:42:23 首次发布
本文探讨如何使用SQL根据时间字段筛选出每组的前N条记录,重点介绍了MySQL中处理此类问题的方法,包括利用临时表和分组查询等技巧。
摘要由CSDN通过智能技术生成