赞
踩
Oracle的分页查询使用很简单,但是我遇到的这个需求跟一般的分页有所不一样,需求可以简单描述为:现在表中列orderNo有很多数据(1、2、3、4......),数据也有重复的,需要去取orderNo在前十的所有数据(实际需求是要根据组数进行分页查询),这里不是取前十条数据,而是取orderNo为1-10含有的所有数据,图示如下:
有同事提出使用开窗函数(具体可参看:https://blog.csdn.net/Dongguabai/article/details/83902993),但是开窗函数好像可以解决分组排序,但是却无法解决我这个需求(也许开窗函数可以解决,只是我不够了解):
在没有找到现成可以使用的函数后最终解决方案为:
查询表中所有的distinct orderNo并且进行分页处理,再将获取到的数据根据orderNo关联表中数据(建议关联查询,不要使用in,可参看:https://blog.csdn.net/Dongguabai/article/details/83898856),最终SQL为:
- select a3.work_order_no workOrderNo,
- a7.fault_name faultName,
- a3.key_value keyValue,
- a3.type_code typeCode,
- a3.tag_name tagName
- from (select * from v_mview_tags a1) a3
- inner join (select a2.work_order_no, a2.fault_type from fa_info a2) a4
- on a4.work_order_no = a3.work_order_no
- inner join (select a5.key_value, a5.tag_id, a5.source, a5.org_id as org_no
- from bdatag_tag_result a5
- where a5.org_id like #{orgId} || '%') a6
- on a3.key_value = a6.key_value
- and a3.tag_id = a6.tag_id
- inner join KN_SP_FAULT a7
- on a7.fault_code = a4.fault_type
-
- inner join (select *
- from (
- select tmp_page.*, rownum row_id
- from (
-
- --查询符合要求的 orderNo
- select distinct aa3.work_order_no workorderno
- from (select * from v_mview_tags aa1) aa3
- inner join (select aa2.work_order_no, aa2.fault_type from fa_info aa2) aa4
- on aa4.work_order_no = aa3.work_order_no
- inner join (select aa5.key_value, aa5.tag_id, aa5.source, aa5.org_id as org_no
- from bdatag_tag_result aa5
- where aa5.org_id like #{orgId} || '%') aa6
- on aa3.key_value = aa6.key_value
- and aa3.tag_id = aa6.tag_id
- inner join KN_SP_FAULT aa7
- on aa7.fault_code = aa4.fault_type
-
-
-
- ) tmp_page
- where rownum <= #{pageSize}
- )
- where row_id > #{pageNum}) tt1
- on tt1.workorderno = a3.work_order_no order by a3.work_order_no
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。