select 
sum(case when A=1 then 1 else 0 end) as A1 ,
sum(case when A=2 then 1 else 0 end) as A2 ,
sum(case when A=3 then 1 else 0 end) as A3 ,
sum(case when A=4 then 1 else 0 end) as A4 
sum(case when B=1 then 1 else 0 end) as B1 ,
sum(case when B=2 then 1 else 0 end) as B2 ,
sum(case when B=3 then 1 else 0 end) as B3 ,
sum(case when B=4 then 1 else 0 end) as B4 
from t1其余的类似写

解决方案 »

  1.   

    select 
    sum(case when a=1 then 1 else 0 end) as a1,
    sum(case when a=2 then 1 else 0 end) as a2,
    ...
    sum(case when b=1 then 1 else 0 end) as b1,
    ...
    sum(case when N=4 then 1 else 0 end) as N4
    from t1
      

  2.   

    select count(*) from t1 group by A,order by A
    然后用BCDE....替换A,要不写一个循环替换字段,但是感觉还不如这样来得快,因为字段不是很多
      

  3.   

    select 
    sum(case when a=1 then 1 else 0 end) as a1,
    sum(case when a=2 then 1 else 0 end) as a2,
    sum(case when a=3 then 1 else 0 end) as a3,
    sum(case when a=4 then 1 else 0 end) as a4,
    sum(case when b=1 then 1 else 0 end) as b1,
    sum(case when b=2 then 1 else 0 end) as b2,
    ......
    sum(case when n=4 then 1 else 0 end) as n4
    from t1
      

  4.   

    select count(*) from
    (select a as  num from table 
     union all select b from table
     union all select c from table
    ...
    ) b
    where b.num=1
      

  5.   

    就是把a,b,c,d..列并成一列 然后求和
      

  6.   

    select 'a' ColName,a,count(*) [count] from t1 group by a
    union all
    select 'b',b,count(*) [count] from t1 group by b
    union all
    select 'c',c,count(*) [count] from t1 group by c
    ......
      

  7.   

    select 'a' ColName,a,count(*) [count] from t1 group by a
    union all
    select 'b',b,count(*) [count] from t1 group by b
    union all
    select 'c',c,count(*) [count] from t1 group by c
    ......select 
    sum(case when a=1 then 1 else 0 end) as a1,
    sum(case when a=2 then 1 else 0 end) as a2,
    sum(case when a=3 then 1 else 0 end) as a3,
    sum(case when a=4 then 1 else 0 end) as a4,
    sum(case when b=1 then 1 else 0 end) as b1,
    sum(case when b=2 then 1 else 0 end) as b2,
    ......
    sum(case when n=4 then 1 else 0 end) as n4
    from t1