问一个SQL表达式的问题
我现在有这样的一个需求,我的表由以下几个字段组成帐号    交易日期    交易时间    次序acc        date             time      order
里面有多条记录现在我想按帐号分类,每个帐号按 date, time, order升序排序。最后输出每个帐号的第一条记录: 如有以下记录001    20080101   0930  1
001    20080101   1010  2
002    20080102   2310  4
002    20080103   0010  1
003    20080101   1010  1最后输出001    20080101   0930  1
002    20080102   2310  4
003    20080101   1010  1如何用一条SQL语句完成上述表达? 谢谢!

解决方案 »

  1.   

    分析函數啊!
    SQL> with a as (select '001' acc,'20080101' date1,'0930' time,1 order1 from dual
      2             union
      3             select '001' acc,'20080101' date1,'1010' time,2 order1 from dual
      4             union
      5             select '002' acc,'20080102' date1,'2310' time,4 order1 from dual
      6             union
      7             select '002' acc,'20080103' date1,'0010' time,1 order1 from dual
      8             union
      9             select '003' acc,'20080101' date1,'1010' time,1 order1 from dual
     10             )
     11  select acc,date1,time,order1 from
     12  (select acc,date1,time,order1,row_number()over(partition by acc order by date1,time,order1) rn from a)
     13  where rn=1
     14  ;
     
    ACC DATE1    TIME     ORDER1
    --- -------- ---- ----------
    001 20080101 0930          1
    002 20080102 2310          4
    003 20080101 1010          1
      

  2.   


    select tt.acc,tt.date,tt.time,tt.order
    from(
    select a.*,row_number() over(partition by acc order by date,time,order) rn
    from table a) tt
    where tt.rn=1