表结构及其数据如下:
name       mytime        weight
--------------------------------
alice     2009-03-09      48
bob       2009-03-09      14
jack      2009-02-01      50
jack      2009-02-01      58我想选取以日期排序后每日最大的纪录,例如上述数据对应的结果是:
alice     2009-03-09      48
jack      2009-02-01      58
请教SQL语句怎么写?

解决方案 »

  1.   

    select a.* from tt a inner join
    (select mytime,max(weight) as ma from tt group by mytime) b
    on a.mytime=b.mytime and a.weight=b.ma
      

  2.   

    or
    select a.name,a.mytime,a.weight from tt a
    left join tt b on a.mytime=b.mytime and a.weight<=b.weight
    group by a.name,a.mytime,a.weight
    having count(b.name)=1
      

  3.   

    select a.* 
    from yourTable a inner join (
    select name,max(weight) as mweight 
    from yourTable 
    group by name
    ) b 
    on a.name=b.name and a.weight=b.mweight
      

  4.   

    select *
    from yourTable a
    where not exists (select 1 from yourTable where name=a.name and weight>a.weight)
      

  5.   


    select *
    from tb t
    where not exists(select * from tb where name=t.name and weight>t.weight)
      

  6.   

    这样有一个要求没能满足哦。。
    我还要实现,如果同一天的结果数量大于1,则只选择一条。。例如
    表结构及其数据如下:
    name      mytime        weight  other
    --------------------------------
    alice    2009-03-09      48     8   
    alice     2009-03-09      48     9
    jack      2009-02-01      50    1
    jack      2009-02-01      58    2可以看到如果选择出来的话,找alice的话,结果是
    name      mytime        weight  other
    --------------------------------
    alice    2009-03-09      48     8   
    alice     2009-03-09      48     9可是我只想要other值比较大的。。
      

  7.   

    或者随便选择一条,不根据other值也行,就是不能重复日期。。每天就一条。。
      

  8.   

    select a.* from tt a inner join
    (select mytime,max(weight) as ma1,MAX(other) as ma1 from tt group by mytime) b
    on a.mytime=b.mytime and a.weight=b.ma AND A.OTHER=B.ma1
      

  9.   

    or
    select a.name,a.mytime,a.weight from tt a
    left join tt b on a.mytime=b.mytime and a.weight <=b.weight
    AND A.OTHER<=B.OTHER
    group by a.name,a.mytime,a.weight
    having count(b.name)=1
      

  10.   

    WWWWA 不错,这个我也想到,如果随便选择一个呢?
      

  11.   

    这个做法如果other的值还是一样的话,纪录还是会重复选择一条砸选呢?
      

  12.   

    呵呵,没有随便,要么最大、要么最小,准备两个SQL语句
      

  13.   

    比如说
    name      mytime        weight   other
    --------------------------------
    alice    2009-03-09      48       4
    alice      2009-03-09      48     4
    jack      2009-02-01      50
    jack      2009-02-01      5这时候查询alice就有两条啊..
    我就要一条呢?
      

  14.   

    好,那我增加一个字段叫 word吧..name      mytime        weight  other   word
    --------------------------------
    alice    2009-03-09      48      4      myself
    alice      2009-03-09      48    4      your
    jack      2009-02-01      50    2       ok
    jack      2009-02-01      5     1        ok选择时候如果重复了,那么就选取长度大的,例如上述是myself那条。。
    如果长度一样,那就比较字符串大小,选择大的。在咋整呢。。拜谢啊
      

  15.   

    如果WORD中有重复的字符,不能做到。用LENGTH取长度
      

  16.   

    word是绝对不重复的话,那么该怎么写呢?
    长度优先,字符串大小次之。。
      

  17.   

    select *
    from yourTable a
    where not exists (
    select 1 from yourTable 
    where name=a.name 
    and (
    weight>a.weight 
    or (weight=a.weight and other>a.other)
    or (weight=a.weight and other=a.other and len(word)>len(a.word))
    or (weight=a.weight and other=a.other and len(word)=len(a.word) and word>a.word)
    )
    )
      

  18.   

    select a.name,a.DTime,a.WORD,A.WGT,COUNT(B.WORD) from tt1 a
    left join tt1 b on a.DTime=b.DTime and a.wGT <=b.WGT
    AND (LEN(A.WORD)<=LEN(B.WORD) OR (A.WORD<=B.WORD))
    group by a.name,a.DTime,a.WORD,A.WGT
      

  19.   

    修改:
    select a.name,a.DTime,a.WORD,A.WGT from tt1 a
    left join tt1 b on a.DTime=b.DTime and a.wGT <=b.WGT
    AND (LEN(A.WORD)<=LEN(B.WORD) AND  (A.WORD<=B.WORD))
    group by a.name,a.DTime,a.WORD,A.WGT
    HAVING COUNT(B.WORD)<=2
      

  20.   

    select a.name,a.DTime,a.WORD,A.WGT from tt1 a
    left join tt1 b on a.DTime=b.DTime and a.wGT <=b.WGT
    AND (LENgth(A.WORD) <=LENgth(B.WORD) AND  (A.WORD <=B.WORD))
    group by a.name,a.DTime,a.WORD,A.WGT
    HAVING COUNT(B.WORD) <=2
    已经测试通过