select sum(case when score > 90 then 1 else 0 end) a, sum(case when score > 80 and score < 90 then 1 else 0 end) b, sum(case when score < 80 then 1 else 0 end) c from table
——给你一个参考,稍微修改一下就可以用了 declare @t table(id int,weight int) insert into @t select 1, 20 insert into @t select 2, 15 insert into @t select 3, 5 insert into @t select 4, 60 insert into @t select 5, 12 insert into @t select 6, 33 insert into @t select 7, 45 insert into @t select 8, 59 insert into @t select 9, 89 insert into @t select 10,110declare @p int set @p=10 select rtrim(p*@p)+'-'+rtrim((p+1)*@p) as p, num from (select (weight/@p) as p,count(*) as num from @t where weight between 10 and 50 group by (weight/@p)) a/*结果: p num ------------------------- ----------- 10-20 2 20-30 1 30-40 1 40-50 1 */
sum(case when score > 80 and score < 90 then 1 else 0 end) b,
sum(case when score < 80 then 1 else 0 end) c
from table
declare @t table(id int,weight int)
insert into @t select 1, 20
insert into @t select 2, 15
insert into @t select 3, 5
insert into @t select 4, 60
insert into @t select 5, 12
insert into @t select 6, 33
insert into @t select 7, 45
insert into @t select 8, 59
insert into @t select 9, 89
insert into @t select 10,110declare @p int
set @p=10
select
rtrim(p*@p)+'-'+rtrim((p+1)*@p) as p,
num
from
(select (weight/@p) as p,count(*) as num from @t where weight between 10 and 50 group by (weight/@p)) a/*结果:
p num
------------------------- -----------
10-20 2
20-30 1
30-40 1
40-50 1
*/