select a,max(date) from table group by a

解决方案 »

  1.   

    select type ,max(date)  from table group by type 
      

  2.   

    select * from tablename a where date=(select max(date) from tablename where Type=a.Type)
      

  3.   

    select * from tablename a where date=(select max(date) from tablename where Type=a.Type)
      

  4.   

    select * from tablename a inner join (select type ,max(date) from tablename group by type ) b on a.type=b.type
      

  5.   

    或者:
    select a.* from tablename a ,(select type ,max(date) as date  from table group by type ) b
    where a.type=b.type and a.date=b.date
      

  6.   

    --如果日期不会重复的话,可以用上面的方法,否则建议用下面的方法:select * from 表 a where id=(select top 1 id from 表 where a.type=type order by 日期 desc)
      

  7.   


    select a.* 
    from tablename a join
         (   select type,max([date]) as mdate  
             from tablename
             group by [type]
          ) b on a.[type]=b.[type] and a.[date]=b.mdate
      

  8.   

    --看楼主给的数据,应该用我上面的方法,下面是数据测试--测试数据declare @t table(ID int,Name varchar(10),Type varchar(1),date datetime)
    insert into @t
    select 1,'XXX','A','2003-2-1'
    union all select 2,'XXX','A','2003-1-1'
    union all select 3,'XXX','A','2003-1-1'
    union all select 4,'XXX','B','2003-2-1'
    union all select 5,'XXX','B','2003-1-1'
    union all select 6,'XXX','B','2003-1-1'
    union all select 7,'XXX','C','2003-2-1'
    union all select 8,'XXX','C','2003-1-1'
    union all select 9,'XXX','C','2003-1-1'--查询
    select * from @t a 
    where id=(select top 1 id from @t where type=a.type order by date desc)/*--测试结果
    ID          Name       Type date                     
    ----------- ---------- ---- -------------------------
    1           XXX        A    2003-02-01 00:00:00.000
    4           XXX        B    2003-02-01 00:00:00.000
    7           XXX        C    2003-02-01 00:00:00.000(所影响的行数为 3 行)
    --*/
      

  9.   

    select type ,max(date)  from table group by type  是可以,但是
    select type ,max(date),name  from table group by type 就不行,说name这个字段没有包含再聚合函数中
      

  10.   

    采用了zjcxc的办法,完全解决了,谢谢!结帖中。
      

  11.   

    --根据楼主的数据,因为同一个type,日期有重复,所以应该用:select * from 表 a 
    where id=(select top 1 id from 表 where type=a.type order by date desc)