现有一个表
A        B             C
1        a             ac
1        a             bc
1        b             dc
2        c             bc
2        c             dc
3        d             mc
3        f             bc
3        m             ac
想得到这么二个结果:
一:A相同的情况下有多少种B(用D表示)
A             D
1             2
2             1
3             3
二:C相同的情况下有多少种B(用E表示)
C             E
ac            2
bc            3
dc            2
mc            1

解决方案 »

  1.   

    select a,count(b)as d from tb group by b
      

  2.   


    1.
    select a,count(b)as d from tb group by a,b
    2.
    select c count(c) as e from tb group by c
      

  3.   

    create table #tb(A varchar(2), B varchar(2), C varchar(2)) 
    insert #tb
    select '1','a','ac' union all
    select '1','a','bc' union all
    select '1','b','dc' union all
    select '2','c','bc' union all
    select '2','c','dc' union all
    select '3','d','mc' union all
    select '3','f','bc' union all
    select '3','m','ac' --A相同的情况下有多少种B(用D表示)
    select a,count(distinct b)d
    from #tb
    group by a/*
    a    d           
    ---- ----------- 
    1    2
    2    1
    3    3(所影响的行数为 3 行)*/
    --C相同的情况下有多少种B(用D表示)
    select c,count(distinct b)e
    from #tb
    group by c/*
    ---- ----------- 
    ac   2
    bc   3
    dc   2
    mc   1(所影响的行数为 4 行)
    */