select b,count(case b when >60 then 1 else 0 end) c6 ,count(case b when >50 then 1 else 0 end) c6,count(case b when >40 then 1 else 0 end) c4 from tablename group by b
上面些错了 这个正确:select count(1) [60以上的],(select count(1) from tablename where b>50) [50以上的],(select count(1) from tablename where b>40)[40以上的] from tablename where b>60
樓主,你把上面的count改成 sum就行了select b,sum(case b when >60 then 1 else 0 end) c6 ,sum(case b when >50 then 1 else 0 end) c6,sum(case b when >40 then 1 else 0 end) c4 from tablename group by b
若再加就列,如何解决? a b aa 001 60 a 002 50 a 003 40 a 004 30 a 005 49 a 006 39 b 007 64 b 008 54 b 010 53 b 011 22 b 012 70 b
aa 60及以上 50及以上 40及以上 a 1 2 4 b 2 4 0
select c,sum(case when b>=60 then 1 else 0 end) as c6 ,sum(case when b>=50 then 1 else 0 end) as c5,sum(case when b>=40 then 1 else 0 end) as c4 from jcb group by caa 60及以上 50及以上 40及以上 a 1 2 4 b 2 4 4为b时,40及以上不应为0,为4
这个正确:select count(1) [60以上的],(select count(1)
from tablename
where b>50) [50以上的],(select count(1)
from tablename
where b>40)[40以上的]
from tablename
where b>60
from tablename
group by b
a b aa
001 60 a
002 50 a
003 40 a
004 30 a
005 49 a
006 39 b
007 64 b
008 54 b
010 53 b
011 22 b
012 70 b
aa 60及以上 50及以上 40及以上
a 1 2 4
b 2 4 0
from jcb
group by caa 60及以上 50及以上 40及以上
a 1 2 4
b 2 4 4为b时,40及以上不应为0,为4