表a有2个字段 
bh  varchar(20)
date datetime记录中bh可以重复,但日期不同,用怎样的SQL语句提取出每一个编号最后一次登记的日期的数据呢?
例原数据:
11 2005-01-01
11 2006-04-01
22 2001-01-01
22 2003-09-02
需要得到的数据
11 2006-04-01
22 2003-09-02
SQL怎么写呀

解决方案 »

  1.   

    select * from a
    where date in
    (
    select max(date)
    from a
    group by bh
    ) as x
      

  2.   

    select t.* from 表a t where not exists(select 1 from 表a where bh=t.bh and date>t.date)
      

  3.   

    select * from a t1
    where not exists (select 1 from a where bh = t1.bh and rq > t1.rq)
      

  4.   

    select t.* from 表a t where t.date=(select max(date) from 表a where bh=t.bh)
      

  5.   

    select t.* from 表a t where t.date=(select top 1 date from 表a where bh=t.bh order by date desc)
      

  6.   

    更正:
    select * from abc
    where c2 in
    (
    select max(c2)
    from abc
    group by c1
    )
      

  7.   

    select bh, max(date) from tablename group by bh
      

  8.   

    declare @ta table (bh  varchar(20),date datetime)insert into @ta select '11',cast('2005-01-01' as datetime) union all select '11',cast('2006-04-01' as datetime) 
    union all select '22',cast('2001-01-01' as datetime) union all select '22',cast('2003-09-02' as datetime)select * from @taselect bh,max(date) from @ta group by bh
      

  9.   

    select bh,max(date ) from 表 as a leftjoin
    (select distant bh from 表) as b on a.bh=b.bh
    group by bh
      

  10.   

    select bh,max(date) from A Group by bh
      

  11.   

    select bh,max(date) from a group by bh
    这样就可以了!