Oracle对多表关联数据进行去重(多对一)
使用窗口函数进行去重
可以实现对于多对一的表数据进行去重
select * from (select taskNo,userCodes,userCode,REPLYTIME,taskStatus from (select d.TASKNO,t.userCode as userCodes,
(select u.userName from 表名 u where u.USERCODE = t.USERCODE) as userCode,rownum row_id,
t.REPLYTIME,
case
when t.STATUS = '0'
then '暂存'
when t.STATUS = '1'
then '待处理'
when to_char(t.REPLYTIME, 'yyyy-MM-ddhh24:mi')< to_char(d.EXPECTTIME, 'yyyy-MM-ddhh24:mi')and t.STATUS = '2'
then '已处理'
end as taskStatus, row_number() over (partition by t.taskNo order by t.REPLYTIME desc) as rn from 表名t left join 表名d on t.taskNo = d.taskNo left join 表名m on d.MEETINGNO = m.MEETINGNO
where d.MEETINGNO = ' ' and rownum <= 10) a where a.rn = 1 and row_id>=1)
注意:上面是错的,如果去重之后需要排序,不能将排序放在去重里面,应该去重完毕之后再进行排序,要不然列表展示会出现问题,展示的和总数不一样。下面正确
select * from (select b.*,rownum row_id from (select taskNo,TASKNAME,INITIATETIME,userCodes,userCode,REPLYTIME,URGETIME
from (select d.TASKNO, d.TASKNAME,d.INITIATETIME,t.userCode as userCodes,
(select u.userName from 表名 u where u.USERCODE = t.USERCODE) as userCode,
t.REPLYTIME,t.URGETIME,row_number() over (partition by t.taskNo order by t.REPLYTIME desc) as rn
from 表名 t
left join 表名d on t.taskNo = d.taskNo
left join 表名m on d.MEETINGNO = m.MEETINGNO
where d.MEETINGNO = #{meetingNo}) a where a.rn = 1)b where rownum <= 10)c where row_id>1