22,209
社区成员
发帖
与我相关
我的任务
分享
select 范围=case when score <30 then '0-30'
when score >=30 and score <60 then '31-60'
when score >=60 and score <90 then '51-90'
when score >=90 then '>90' end
,X=sum(case when type='x' then 1 else 0 end)
,Y=sum(case when type='y' then 1 else 0 end)
,Z=sum(case when type='z' then 1 else 0 end)
,W=sum(case when type='w' then 1 else 0 end)
from A
group by case when score <30 then '0-30'
when score >=30 and score <60 then '31-60'
when score >=60 and score <90 then '51-90'
when score >=90 then '>90' end
[code=SQL]create table `A`(`id` int,`name` varchar(1),`type` varchar(1),`score` int);
insert `A` values(1,'a','x',0);
insert `A` values(2,'b','y',54);
insert `A` values(3,'a','z',87);
insert `A` values(4,'c','z',0);
insert `A` values(5,'b','w',20);
insert `A` values(6,'c','x',75);
insert `A` values(7,'d','y',32);
insert `A` values(8,'e','w',23);
select
case when score <30 then '0-30'
when score >=30 and score <60 then '31-60'
when score >=60 and score <90 then '51-90'
when score >=90 then '>90' end as `范围`,
sum(case when type='x' then 1 else 0 end) as X,
sum(case when type='y' then 1 else 0 end) as Y,
sum(case when type='z' then 1 else 0 end) as Z,
sum(case when type='w' then 1 else 0 end) as W
from A
group by case when score <30 then '0-30'
when score >=30 and score <60 then '31-60'
when score >=60 and score <90 then '51-90'
when score >=90 then '>90' end
/**
范围 X Y Z W
0-30 1 0 1 2
31-60 0 2 0 0
51-90 1 0 1 0
**/
[/code]