表如下:
service    s_code       disk_name       used_disk       read_time
DB        1        /dev/shm           1037380           20060714 08:40  //        
DB        1        /boot                 101086            20060714 08:40  //
DB        1        /tmp                 101086            20060714 08:40  //
DB        1        /dev/shm           1037380           20060714 08:39
DB        1        /boot                 101086            20060713 15:39
DB        1        /dev/shm           1037380           20060713 15:39
DB        1        /boot                 101086           20060713 15:38
DB        1        /dev/shm           1037380           20060713 15:38
DB        1        /boot                 101086           20060713 15:36
DB        1        /dev/shm           1037380           20060713 15:36
DB        1        /dev/shm           1037380           20060713 15:35
DB        1        /boot                 101086           20060713 15:35DB        2        /boot                 101086           20060713 15:37  //
DB        2        /dev/shm           1037380           20060713 15:37  //WEB        1        /boot                 101086           20060713 15:46  //
WEB        1        /dev/shm           1037380           20060713 15:46  //        
WEB        1        /serv                 1037380           20060713 15:46  //
WEB        1        /home                 101086           20060713 15:46  //
WEB        1        /boot                 101086           20060713 15:43
WEB        1        /dev/shm           1037380           20060713 15:43
WEB        1        /boot                 101086           20060713 15:42
WEB        1        /dev/shm           1037380                 20060713 15:42需求是这样的:以service   s_code分组 , 显示read_time最大的N条记录.
这个N对不同的service   s_code分组是不同的, 如上DB        1是三条记录,
DB 2两条记录, WEB 1就是四条记录.
规律就是分组后取最大的read_time有相同值的那几条记录.
SQL不是很熟,还请高手指点!

解决方案 »

  1.   

    select * from test,
    (select s_code aa,max(read_time) bb from test group by s_code) t
    where test.s_code=t.aa,test.read_time=t.bb;
      

  2.   

    select a1.*
      from 表 a1,
           (select service, s_code, max(t.read_time) read_time
              from 表 t
             group by t.service, t.s_code) a2
     where  a1.service = a2.service 
       and  a1.s_code = a2.s_code 
       and  a1.read_time = a2.read_time