分类排列如 表tt
at   rt
203  20050403
203  20060408
204  20080506
204  20090504我要取不同的at 和rt的时间最大的那个  怎么写sql  谢谢!

解决方案 »

  1.   

    --> 测试数据:@表tt
    declare @表tt table([at] int,[rt] datetime)
    insert @表tt
    select 203,'20050403' union all
    select 203,'20060408' union all
    select 204,'20080506' union all
    select 204,'20090504'select at,MAX(rt) from @表tt group by at
      

  2.   

    select at,max(rt) from tt group by at..
      

  3.   

    select 
        at,
        MAX(rt) as rt
    from 
        表tt 
    group by 
        at
      

  4.   


    if exists(select * from sysobjects where [name]='tb')
    drop table tb
    go
    create table tb([at] int,[rt] datetime)
    go
    insert into tb([at],[rt])
    (
    select 203,'20050403' union
    select 203,'20060408' union
    select 204,'20080506' union
    select 204,'20090504'
    )
    go
    select [at],max([rt]) from tb group by [at]
    go查询结果
    at          
    ----------- -----------------------
    203         2006-04-08 00:00:00.000
    204         2009-05-04 00:00:00.000
      

  5.   

    at rt         ST    WT
    203 20050403   7    45
    203 20060408   8    11
    204 20080506   9    55
    204 20090504   90   22我要取不同的at 和rt的时间最大的那个 怎么写sql 谢谢!增加了几列后 group by 会提示
      

  6.   

    select *
    from tt t
    where not exists(select 1 from tt where at = t.at and rt > t.rt)
      

  7.   


    Select at,CONVERT(DateTime,MAX(rt), 120) From 表tt Where IsDate(rt)=1 Group By at