io_name io_date
――――――――――――――――――
张三 2006-12-15 8:50:00
张三 2006-12-15 8:03:00
张三 2006-12-15 8:04:00
张三 2006-12-15 8:20:00
李四 2006-12-15 8:20:00
李四 2006-12-15 9:20:00
李四 2006-12-15 8:25:00
李四 2006-12-15 8:20:00
李四 2006-12-15 4:20:00
王五 2006-12-15 8:20:00
王五 2006-12-15 9:20:00
王五 2006-12-15 8:25:00
王五 2006-12-15 8:20:00
王五 2006-12-15 4:20:00如何分别找出每个人的最早时间和最晚时间?例如:io_name io_data_first          io_data_last
张三 2006-12-15 8:03:00 2006-12-15 8:50:00
李四 2006-12-15 4:20:00 2006-12-15 9:20:00
王五 2006-12-15 4:20:00 2006-12-15 9:20:00

解决方案 »

  1.   

    declare @a table(io_name varchar(10), io_date smalldatetime)insert @a select '张三', '2006-12-15 8:50:00'
    union all select '张三', ' 2006-12-15 8:03:00'
    union all select '张三', ' 2006-12-15 8:04:00'
    union all select '张三', ' 2006-12-15 8:20:00'
    union all select '李四', ' 2006-12-15 8:20:00'
    union all select '李四', ' 2006-12-15 9:20:00'
    union all select '李四', ' 2006-12-15 8:25:00'
    union all select '李四', ' 2006-12-15 8:20:00'
    union all select '李四', ' 2006-12-15 4:20:00'
    union all select '王五', ' 2006-12-15 8:20:00'
    union all select '王五', ' 2006-12-15 9:20:00'
    union all select '王五', ' 2006-12-15 8:25:00'
    union all select '王五', '2006-12-15 8:20:00'
    union all select '王五', '2006-12-15 4:20:00'select io_name,io_data_first=min(io_date),io_data_last=max(io_date) from @a group by io_name
      

  2.   

    select a.io_name,min(b.io_date) io_data_first,max(b.io_date) io_data_last 
       from a,(select * from tab orde by io_name,io_date) b
       where a.io_name=b,io_name
      

  3.   

    select io_name,min(io_date) io_data_first,max(io_date) io_data_last from tablename group by io_name
      

  4.   

    select a.io_name,min(a.io_date) as io_date_first,max(b.io_date) as io_date_last 
    from tablename a 
    inner join tablename b on a.io_name=b.io_name 
    group by a.io_name
      

  5.   

    select io_name,min(io_date) as io_date_first,max(io_date) as io_date_last 
    from tablename
    group by io_name
      

  6.   

    select io_name,io_data_first=min(io_date),io_data_last=max(io_date) from [TABLE] group by io_name