34,587
社区成员
发帖
与我相关
我的任务
分享
create table tb(name varchar(10), type int, status varchar(10))
insert into tb values('lbob' ,1 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('Xu ' ,0 ,'Normal')
insert into tb values('Xu ' ,1 ,NULL)
insert into tb values('lbob' ,1 ,NULL)
insert into tb values('gyi ' ,1 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('Tim ' ,1 ,'Normal')
insert into tb values('Jock' ,1 ,'Normal')
insert into tb values('Nick' ,0 ,'Normal')
insert into tb values('Tim ' ,1 ,'Normal')
insert into tb values('Jock' ,1 ,'Normal')
insert into tb values('Nick' ,0 ,'Normal')
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,1 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,1 ,NULL)
insert into tb values('lbob' ,1 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,1 ,'Normal')
insert into tb values('Xu ' ,1 ,'Absent')
insert into tb values('lbob' ,0 ,NULL)
insert into tb values('lbob' ,1 ,'Late')
insert into tb values('Xu ' ,0 ,'Absent')
insert into tb values('lbob' ,1 ,NULL)
insert into tb values('gyi ' ,0 ,NULL)
go
select
[Normal、Late条数] = (select count(1) from tb where status = 'Normal' or status = 'Late'),
[总条数] = (select count(1) from tb),
[百分比] = cast((select count(1) from tb where status = 'Normal' or status = 'Late')*100.0/(select count(1) from tb) as decimal(18,2))
drop table tb
/*
Normal、Late条数 总条数 百分比
------------- ----------- --------------------
9 29 31.03
(所影响的行数为 1 行)
*/
select
sum(case when status in('Normal','Late') then 1 else 0 end) as [Normal、Late条数],
count(1)总条数,
ltrim(sum(case when status in('Normal','Late') then 1 else 0 end)*100/count(1))+'%'
from
tb
where xxx
select sum(case when status='Normal' then 1 else 0 end)Normal条数,
sum(case when status='Late' then 1 else 0 end)Late条数,
count(*)总条数
from tb