数据结构如下:if object_id('tempdb.dbo.#A') is not null drop table #A
create table #A (ID INT IDENTITY(1,1),C1 VARCHAR(100))
insert into #A
select 'A' union all
select 'B' union all
select 'C' union all
select 'A' union all
select 'B' union all
select 'A'想得到的结果如下:
 c1    Num
  A     3
  B     2
  C     1

解决方案 »

  1.   

    if object_id('tempdb.dbo.#A') is not null drop table #A
    create table #A (ID INT IDENTITY(1,1),C1 VARCHAR(100))
    insert into #A
    select 'A' union all
    select 'B' union all
    select 'C' union all
    select 'A' union all
    select 'B' union all
    select 'A'select c1 ,count(1) num from #A group by c1
    /*
    c1     num
    ------ -----------
    A      3
    B      2
    C      1
      

  2.   

    select c1,num=count(c1) from tb 
    group by c1
    order by num desc
      

  3.   

    ---测试数据
    if object_id('tempdb.dbo.#A') is not null drop table #A
    create table #A (ID INT IDENTITY(1,1),C1 VARCHAR(100))
    insert into #A
    select 'A' union all
    select 'B' union all
    select 'C' union all
    select 'A' union all
    select 'B' union all
    select 'A'
    ---查询
    select c1,num=count(c1) from #A 
    group by c1
    order by num desc
    --结果
    /*
    c1    num
    ---   -----
    A 3
    B 2
    C 1*/
      

  4.   

    就一个group by ,太简单了吧,楼主也不看看书?