如题,比如:
用户a,时间,其它字段
用户b,时间,其它字段
用户a,时间,其它字段
用户c,时间,其它字段
想取每个用户,每天时间最晚的那条记录。表中时间格式,年月日小时分秒都有。
最后结果是
用户a,时间,其它字段
用户b,时间,其它字段
用户c,时间,其它字段
谢谢

解决方案 »

  1.   

    select *
    from (select t.*, row_number() over(partition by trunc(time) order by time desc) rn from t)
    where rn = 1;
      

  2.   

    partition 后少了用户id
    shiyiwan兄好久不见``
      

  3.   

    呵呵,对的,应该是partition by trunc(time),id最近在忙啥呢?
      

  4.   

    SELECT *
      FROM test5 a
     WHERE (a.username, a.time) IN
           (SELECT t.username, MAX(TIME) FROM test5 t GROUP BY t.username, trunc(TIME));
      

  5.   

    select *
    from table1 a
    where not exists (select 1 from table1 where 用户=a.用户 and 时间>a.时间);
      

  6.   

    我都一一试过了,shiyiwan和ACMAIN_CHM的答案不对,我要的是,每个用户,每天的最晚记录。你们的SQL,只保留了用户最后一天的最晚记录。
    tangren的回答是正解。谢谢所有回答的朋友。
      

  7.   

    shiyiwan的答案,把3楼时的补充partition by trunc(time),id 加上,也对。
    十分抱歉,刚才没注意。
      

  8.   


    如果不只是从Test5里面而是从Test6、Test5里面查出字段来重新组合且time只是test6里的一个字段,该咋个处理呢