现有表TBA如下,ZA只能输1或不输,ZB只能输2或不输.........ZE只能输5或不输id   ZA   ZB   ZC   ZD    ZE
 1   1    2                5
 2        2          4     5
 3   1         3     4     
 4        2    3           3
 5        2          4     5 
要统计各个数字出现的次数select count(za),count(zb),count(zc),count(zd),count(ze) from TBA group by za,zb,zc,zd,ze以上写法怎么不正确?
   

解决方案 »

  1.   

    select sum(case when za=1 then 1 else 0 end)za次數,
           sum(case when zb=2 then 1 else 0 end)zb次數,
           sum(case when zc=3 then 1 else 0 end)zc次數,
           sum(case when zd=4 then 1 else 0 end)zd次數,
           sum(case when za=5 then 1 else 0 end)ze次數
    from tba
      

  2.   

     create table tb (a int,b int,c int,d int,e int)insert into tb values (1,2,3,4,5)
    insert into tb values (null,null,null,null,null)
    insert into tb values (null,2,null,4,5)
    insert into tb values (1,null,3,4,null)
    insert into tb values (null,2,3,null,5)
    insert into tb values (1,2,null,null,5)
     select count(a),count(b),count(c),count(d),count(e) from TB 如果你说的不输是"NULL" 那就这样即可
      

  3.   

    高手 能否用count    group by实现?
      

  4.   

    我的是ACCESS数据库 好像不行
      

  5.   

    1楼的方法最好;  我先提供个最傻的方法:
    select a.count1,b.count2,c.count3,d.count4,e.count5 from 
    (
    select count(id1) as count1,1as id from t7 where id1=1
    ) a
     left join 
    (
    select count(id2) as count2,1as id from t7 where id2=2
    ) b
     on a.id=b.id 
     left join
    (
    select count(id3) as count3,1as id from t7 where id3=3 
    ) c
     on c.id=a.id
     left join
    (
    select count(id4) as count4,1as id from t7 where id4=4 
    ) d
     on d.id=a.id
     left join
    (
    select count(id5) as count5,1as id from t7 where id5=5
    ) e
    on e.id=a.id
      

  6.   

    select sum(tt.l1),sum(tt.l2),sum(tt.l3),sum(tt.l4),sum(tt.l5) from 
    (
    select count(convert(int,id1))as l1  ,count(convert(int,id2)) as l2,count(convert(int,id3)) as l3,
    count(convert(int,id4)) as l4,count(convert(int,id5)) as l5
    from t7 
    where id1=1 or id2=2 or id3=3 or id4=4 or id5=5
    group by id1,id2,id3,id4,id5
    ) tt这是个用group by + Count的方法,  也够傻的了,还是用1楼的吧