例如:表A
id  name value date
'001' 张三 aa 2009-12-1
'001' 张三 bb 2009-12-2
'001' 张三 cc 2009-12-3
'004' 李四 xx 2009-12-4
'004' 李四 yy 2009-12-5
'004' 李四 zz 2009-12-6
'007' 王五 mm 2009-12-7
'007' 王五 nn 2009-12-8求得到下面结果,取出每个用户date列最大日期的记录行
id  name value date
'001' 张三 cc 2009-12-3
'004' 李四 zz 2009-12-6
'007' 王五 nn 2009-12-8有没有高手会这个sql,我的数据库是sqlsever2000

解决方案 »

  1.   

    select *
    from tb A
    where not exists(select 1 from tb B where A.id=B.id and A.name=B.name and A.date<B.date)
      

  2.   

    select t.* from a t where date = (select max(date) from a where id = t.id)
    select t.* from a t where not exists (select 1 from a where id = t.id where date > t.date)
      

  3.   


    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    go 
    create table [TB]([id] varchar(3),[name] varchar(4),[value] varchar(2),[date] datetime)
    insert [TB]
    select '001','张三','aa','2009-12-1' union all
    select '001','张三','bb','2009-12-2' union all
    select '001','张三','cc','2009-12-3' union all
    select '004','李四','xx','2009-12-4' union all
    select '004','李四','yy','2009-12-5' union all
    select '004','李四','zz','2009-12-6' union all
    select '007','王五','mm','2009-12-7' union all
    select '007','王五','nn','2009-12-8'
    --------------开始查询--------------------------select * from [TB] T WHERE datE=(SELECT MAX(date) FROM TB WHERE name=T.name)
    ----------------结果----------------------------
    /* (所影响的行数为 8 行)id   name value date                                                   
    ---- ---- ----- ------------------------------------------------------ 
    001  张三   cc    2009-12-03 00:00:00.000
    007  王五   nn    2009-12-08 00:00:00.000
    004  李四   zz    2009-12-06 00:00:00.000(所影响的行数为 3 行)
    */
      

  4.   

    create table a (id varchar(10), name varchar(10),value varchar(10),date datetime)
    insert into a values('001', '张三', 'aa', '2009-12-1') 
    insert into a values('001', '张三', 'bb', '2009-12-2') 
    insert into a values('001', '张三', 'cc', '2009-12-3') 
    insert into a values('004', '李四', 'xx', '2009-12-4') 
    insert into a values('004', '李四', 'yy', '2009-12-5') 
    insert into a values('004', '李四', 'zz', '2009-12-6') 
    insert into a values('007', '王五', 'mm', '2009-12-7') 
    insert into a values('007', '王五', 'nn', '2009-12-8')
    goselect t.* from a t where date = (select max(date) from a where id = t.id) order by t.idselect t.* from a t where not exists (select 1 from a where id = t.id and date > t.date) order by t.id
    drop table a /*
    id         name       value      date                                                   
    ---------- ---------- ---------- ------------------------------------------------------ 
    001        张三         cc         2009-12-03 00:00:00.000
    004        李四         zz         2009-12-06 00:00:00.000
    007        王五         nn         2009-12-08 00:00:00.000(所影响的行数为 3 行)id         name       value      date                                                   
    ---------- ---------- ---------- ------------------------------------------------------ 
    001        张三         cc         2009-12-03 00:00:00.000
    004        李四         zz         2009-12-06 00:00:00.000
    007        王五         nn         2009-12-08 00:00:00.000(所影响的行数为 3 行)*/
      

  5.   

    select t1.*
    from a t1 inner join (select id,max(date) as max_date from a group by id) t2
    on t1.id=t2.id and t1.date=t2.max_date
      

  6.   

    4楼改过来了.
    其中把and 写成 where了,不好意思.