表如下:
id   ac    ac_value
1    ww    7
2    ww    12
3    ss    9
4    ss    6
5    ss    26
6    dd    3最后得到的结果是每ac_value字段中的最大值,结果如下:
2    ww    12
5    ss    26
6    dd    3
请问这个SQL语句这么写啊?

解决方案 »

  1.   

    select ac,max(ac_value) from table_name group by ac不过只能查出ac 和ac_value,查id不是很好查,毕竟要考虑到ac_value有相同值得情况。
      

  2.   

    select a.* from ac_tb a , ac_tb b, ac_tb c 
    where a.id>b.id and a.id>c.id and 
    b.ac>a.ac and b.ac>c.ac and 
    c.ac_value>a.ac_value and c.ac_value>b.ac_value
      

  3.   

      select a.id ,a.ac,a.ac_value  from   ac_tb   a   ,   ac_tb   b 
    where 
    (a.ac_value> b.ac_value and a.ac=b.ac and a.id>b.id) or(select count(*) from ac_tb c where c.ac=a.ac)=1 group by a.id ,a.ac, a.ac_value 
    性能低了些你可以改为union
      

  4.   

    上面那个有点bug
    select d.* from 
    (select max(a.id) as id  from   ac_tb   a   ,   ac_tb   b 
    where (a.ac_value> b.ac_value and a.ac=b.ac or(select   count(*)   from   ac_tb   c   where   c.ac=a.ac)=1  )
    group by a.ac) c join ac_tb d on c.id=d.id
    这个应该没问题了
      

  5.   


    create table myTable (
        id int,
        ac char(10),
        ac_value int
    )goinsert into myTable select 1,'ww',7
    union all select 2,'ww',12
    union all select 3,'ss',9
    union all select 4,'ss',6
    union all select 5,'ss',26
    union all select 6,'dd',3gocreate function dbo.get(@value int)
    returns int
    as
    begin
        declare @rv int
        set @rv=0
        select @rv=id from myTable where ac_value=@value
        return @rv
    end
    goselect dbo.get(max(ac_value)) as id,ac,max(ac_value) as max_value from myTable group by ac order by iddrop function get
    drop table myTable/***
     (所影响的行数为 6 行)id          ac         max_value   
    ----------- ---------- ----------- 
    2           ww         12
    5           ss         26
    6           dd         3(所影响的行数为 3 行)
     ***/
      

  6.   

    select( select id from mytable where max(m.ac_value) =ac_value )as id, ac,max(ac_value) as max_value from mytable m group by ac order by id
      

  7.   

    Select id, ac, max(ac_value) From Table_Name Group By ac