有个表ta(A,B,C)假设数据如下A  B  C
1  x  10
1  y  100
1  h  30
6  c  80
6  z  50要把字段A中相同的并且字段C中最大的值检索出来语句该怎么写上面的结果应该是:A  B  C
1  y  100
6  c  80

解决方案 »

  1.   

    select *
    from ta t
    where not exists(select 1 from ta where a = t.a and c >t.c)
      

  2.   

    这个直接用max,group by 是不行的,1楼正解
      

  3.   


    select *
    from ta t
    where not exists(select 1 from ta where a = t.a and c >t.c)c参考:http://blog.csdn.net/lihan6415151528/archive/2009/02/02/3857884.aspx
      

  4.   

    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (a INT,b VARCHAR(2),c INT)
    INSERT INTO [tb]
    SELECT 1,'x',10 UNION ALL
    SELECT 1,'y',100 UNION ALL
    SELECT 1,'h',30 UNION ALL
    SELECT 6,'c',80 UNION ALL
    SELECT 6,'z',50
    goselect * from tb a 
    where 
    not exists 
    (select 1 from tb where a=a.a and c>a.c)
    /*a           b    c
    ----------- ---- -----------
    1           y    100
    6           c    80(2 行受影响)*/