表A如下:
列cola colb colc
   a   3     1
   a   2     2  <--
   b   9     3
   b   10    4  <--我需要根据Cola分组,然后在同组内按ColC排序,取ColC值最大的那行的ColB值,即
如何通过查询得到这样的结果:
列cola colb colc
   a   2     2
   b   10    4
  

解决方案 »

  1.   

    select a.cola,colb=(select colb from tb where colc=max(a.colc)),max(a.colc) from tb  a group by a.cola
      

  2.   

    select * from ta a where not exists(select 1 from ta where cola=a.cola and colc>a.colc)
      

  3.   

    SELECT *
    FROM tb AS A
    WHERE NOT EXISTS(SELECT * FROM tb WHERE COLA=A.COLA AND COLC>A.COLC)
    ORDER BY COLA;
      

  4.   

    select a.* from tb a where colc=(select top 1 colc from tb  where cola=a.cola order by colc desc )
      

  5.   

    create table a (cola char(1),colb int,colc int)
    go
    insert into a
    select 'a',3,1
    union all
    select 'a',2,2
    union all
    select 'b',9,3
    union all
    select 'b',10,4
    go
    select a.cola,a.colb,a.colc
    from a
      inner join (
        select cola,max(colc) as colc
        from a
        group by cola
      ) list on list.cola = a.cola and list.colc = a.colc
    order by a.cola