select field1, cnt from 
(select field1,count(*) cnt,
max((select max(sn) from test b where a.field1 = b.field1)) sn 
from test a
group by field1
) order by sn

解决方案 »

  1.   

    直接用 select field1 ,count(1) from tb group by field1 order by sn;
      

  2.   

    直接group by ... order by 即可。
      

  3.   

    select field1, cnt from 
    (select field1,count(*) cnt,
    max((select max(sn) from test b where a.field1 = b.field1)) sn 
    from test a
    group by field1
    ) order by sn
      

  4.   

    直接用不行,提示sn不是group by表达式
      

  5.   

    我现想对结果按字段sn排序,想得到
    bb    1
    cc    2
    aa    2在原表中,aa的sn有两个值6,2 如何排? 不好直接这么做的. select tb.field1,count(tb.field2) from tb group by tb.field1 order by 2;