select A,max(B) from tablename group by A

解决方案 »

  1.   

    declare @Tab table(a varchar(10),b datetime)
    insert @tab select 'w','2005-10-1'
    union all select 'w','2005-09-20'
    union all select 'd','2003-01-01'
    union all select 'd','2002-10-05'select a,min(b)as b from @tab group by a
    結果:a          b                                                      
    ---------- ------------------------------------------------------ 
    d          2002-10-05 00:00:00.000
    w          2005-09-20 00:00:00.000
      

  2.   

    --上面写错了。
    declare @tab table(A varchar(20),B datetime)insert @tab values('w','2005-10-1')
    insert @tab values('w','2005-09-20')
    insert @tab values('d','2003-01-01')
    insert @tab values('d','2002-10-05')
    select A,min(B) from @tab group by A--jieguo d                    2002-10-05 00:00:00.000
    w                    2005-09-20 00:00:00.000
      

  3.   

    或者:
    select a,convert(varchar(10),min(b),20)as b from @tab group by a
    結果:
    a          b          
    ---------- ---------- 
    d          2002-10-05
    w          2005-09-20