--> 测试时间:2009-07-07 11:36:50
--> 我的淘宝: http://shop36766744.taobao.com/if object_id('[tab]') is not null drop table [tab]
create table [tab]([branum] varchar(5),[bralong] varchar(5))
insert [tab]
select '0799V','0799' union all
select '0091V','0091V' union all
select '0797V','0091V' union all
select '0797','0091V' union all
select '0204','0200V' union all
select '0205','0200V' union all
select '0206','0200V' union all
select '0207','0200V' union all
select '0243','0250V' union all
select '0244','0250V' union all
select '0247','0250V' union all
select '0249V','0250V'select * from [tab]select count(bralong),bralong from tab group by bralong
order by count(bralong)
/*
            bralong 
----------- ------- 
1           0799
3           0091V
4           0200V
4           0250V(所影响的行数为 4 行)*/drop table tab

解决方案 »

  1.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-07-07 11:40:15
    ---------------------------------
    --> 生成测试数据表-tbif not object_id('tb') is null
    drop table tb
    Go
    Create table tb([branum] nvarchar(5),[bralong] nvarchar(5))
    Insert tb
    select '0799V','0799' union all
    select '0091V','0091V' union all
    select '0797V','0091V' union all
    select '0797','0091V' union all
    select '0204','0200V' union all
    select '0205','0200V' union all
    select '0206','0200V' union all
    select '0207','0200V' union all
    select '0243','0250V' union all
    select '0244','0250V' union all
    select '0247','0250V' union all
    select '0249V','0250V'
    Go
    --Select * from tb-->SQL查询如下:
    select cnt=count(1),[bralong] from tb group by [bralong] order by 1/*
    cnt         bralong
    ----------- -------
    1           0799
    3           0091V
    4           0200V
    4           0250V(4 行受影响)
    */
      

  2.   

    select count(bralong) as [num],
    bralong from tab group by bralong
    order by count(bralong)