字段一 字段二  字段三
     tttt one UploadedPhoto/20060419150815.jpg 2006-4-19 15:08:16
tttt ter UploadedPhoto/20060419150825.jpg 2006-4-19 15:08:26
tttt asdf UploadedPhoto/20060419150833.jpg 2006-4-19 15:08:34
yyyyyyy sdf UploadedPhoto/20060419153429.jpg 2006-4-19 15:34:29
yyyyyyy sdfsd UploadedPhoto/20060419153438.jpg 2006-4-19 15:34:39
yyyyyyy 我的照片 UploadedPhoto/20060419153458.jpg 2006-4-19 15:34:58
如果按ttt公组,我只想要每一组的头一条记当
如果用这个SQL语句,select 
字段一 ,字段二  ,字段三 from 表 group by 字段一
是不正确的,请问用什么方法?

解决方案 »

  1.   

    declare @t table(a varchar(20),b varchar(20),c varchar(40),d varchar(20))
    insert into @t select 'tttt','one','UploadedPhoto/20060419150815.jpg','2006-4-19 15:08:16'
    union all select 'tttt','ter','UploadedPhoto/20060419150825.jpg','2006-4-19 15:08:26'
    union all select 'tttt','asdf','UploadedPhoto/20060419150833.jpg','2006-4-19 15:08:34'
    union all select 'yyyyyyy','sdf','UploadedPhoto/20060419153429.jpg','2006-4-19 15:34:29'
    union all select 'yyyyyyy','sdfsd','UploadedPhoto/20060419153438.jpg','2006-4-19 15:34:39'
    union all select 'yyyyyyy','我的照片','UploadedPhoto/20060419153458.jpg','2006-4-19 15:34:58'select [id]=identity(int,1,1),* into # from @t
    select * from # a where not exists(select 1 from # where id<a.id and a=a.a)
    drop table #
    --这样?
      

  2.   

    select * from 表 where not exists(select 1 from 表 where 字段一=a.字段一 and 字段二=a.字段二,字段三=a.字段三 and 时间字段<a.时间字段)or select 字段一,max(字段二) as 字段二,max(字段三) as 字段三 from 表 group by 字段一
      

  3.   

    select * from 表 where not exists(select 1 from 表 where 字段一=a.字段一 and 时间字段<a.时间字段)改一下上面第一个