我现有一个表,其中有两个字段,如下:字段名     类型
name       varchar[50]
date       datetime表中数据如下:name       date
A          2003-05-08
B          2003-07-02
C          2003-09-03
A          2003-07-05
B          2003-03-03现在需要写一条SQL语句,让它返回下表:
name       date
A          2003-07-05
B          2003-07-02
C          2003-09-03也就是说,让它返回所有数据,但如果 name 字段有重复的,则只返回相对应的 date 字段中数据最大的一个
这条SQL语句应该怎么写?

解决方案 »

  1.   

    select Name,max(date) from 表名 Group by Name
      

  2.   

    select [name],max([date]) as maxDate
    from tablename
    group by [name]注意,尽量避免用保留字作为字段名。
      

  3.   

    Select Name,Max(Date) as Date From Table名 Group By Name
      

  4.   

    Select Name,Max(Date),其它字段 as Date From Table名 Group By Name,其它字段
      

  5.   

    比如我现在的字段一共是: name date other1 other2 other3
    请给出SQL语句,谢谢~
      

  6.   

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

  7.   

    原表为:
    name    date         other1    other2    other3
    A       2003-05-08   ABC       DEF       123
    B       2003-07-02   kkk       sss       888
    C       2003-09-03   asdf      9876      1212
    A       2003-07-05   sss       kkk       ddd
    B       2003-03-03   456       000       wqq返回表为:
    name    date         other1    other2    other3
    A       2003-07-05   sss       kkk       ddd
    B       2003-07-02   kkk       sss       888
    C       2003-09-03   asdf      9876      1212如何写SQL语句?
      

  8.   

    Group By 是用来分组,
    具体看看其它字段,要和的就合(sum),要最小就用min,要分组就加在Group by 后面用“,”分开
      

  9.   

    Group By 是用来分组,
    具体看看其它字段,要和的就合(sum),要最小就用min,要分组就加在Group by 后面用“,”分开
      

  10.   

    Select Name,Max(Date) as Date,other1,other2,other3 From TableName Group By Name,other1,other2,other3