查询重复数据只显示一条并且在规定范围时间内

先看下我的测试数据库

每一个pid都代表一个人员id,这张表下有n条同样的id数据但是时间可以不同

去除重复数据只显示一条 在2020-07-01 00:00:00 -  2020-10-31 23:59:59这个时间段的数据

sql代码:

select *
  from st_death t
 where 1 = 1
   and t.id in (select Max(id) from st_death t group by pid) 
   and 
    (to_char(t.VERIFICATION_TIME, 'YYYY-MM-DD hh:mm:ss')    >='2020-07-01 00:00:00' and to_char(t.VERIFICATION_TIME, 'YYYY-MM-DD hh:mm:ss') <= '2020-10-31 23:59:59') order by t.ssqh,t.verification_time

其中

 and t.id in (select Max(id) from st_death t group by pid) 

作用:去除重复数据显示一条

  我也是参考其他大佬的例子如果有更好的可以在下方评论。。。

原文地址:https://www.cnblogs.com/wolf-shuai/p/13953997.html