表结构如下(id,姓名,操作,时间)其中ID是主键,“操作”的取值为增加、修改、删除,“时间”为操作时间。
现要查询如下表示结果:指定时间内的记录,同一姓名的人做过多次操作,取最近的一条。
求解!!!

解决方案 »

  1.   

    select a.*
    from (select id,姓名,操作,时间,row_number() over(partiton by 姓名 order by 时间 desc) rn
    from table 
    where 时间=?) a
    where a.rn=1
      

  2.   

    select max(id),max(姓名),操作,max(时间) from 表 where between 小时间 and 大时间
      

  3.   

    select id,姓名,操作,时间
    from tb where 时间 in(select 时间 from tb  where 时间>=dt and 时间<=dt2 group by 姓名)
      

  4.   

    select a.*
    from (select id,姓名,操作,时间,row_number() over(partiton by 姓名 order by 时间 desc) rn
    from tb_name 
    where 姓名=?) a
    where a.rn=1
      

  5.   

    select a.*
      from (select id,
                   姓名,
                   操作,
                   时间,
                   row_number() over(partiton by 姓名 order by 时间 desc) rn
              from table
             where 时间 between XXX and YYY) a
     where a.rn = 1
      

  6.   

    select id,姓名,操作,时间
    from tb where 时间 in(select max(时间) from tb  where 时间>=dt and 时间<=dt2 group by 姓名)
      

  7.   

    select t1.id,t1.姓名,t1.操作,t1.时间
    from tb t1 where t1.时间 in
    (select max(t2.时间) from tb  t2. where t1.id =  t2.id and t2.时间>=dt and t2.时间<=dt2 group by t2.id)