select 班号,count(*) 班级人数,
sum(case when 迟到否=0 then 1 else 0 end) 迟到人数,
sum(case when 迟到否=1 then 1 else 0 end) 未迟到人数
from table0
group by 班号
sum(case when 迟到否=0 then 1 else 0 end) 迟到人数,
sum(case when 迟到否=1 then 1 else 0 end) 未迟到人数
from table0
group by 班号
(
班号 varchar(10),
学生ID int,
迟到否 bit,
)
要求查询得到:
班号 班级人数 迟到人数 未迟到人数
select
班号,
班级人数 = (select count(*) from table0 where 班号=a.班号),
迟到人数 = (select count(*) from table0 where 班号=a.班号 and 迟到否=true),
未迟到人数 = (select count(*) from table0 where 班号=a.班号 and 迟到否=false)
from table0 a group by 班级人数,迟到人数,未迟到人数
班号,
班级人数 = (select count(*) from table0 where 班号=a.班号),
迟到人数 = (select count(*) from table0 where 班号=a.班号 and 迟到否=true),
未迟到人数 = (select count(*) from table0 where 班号=a.班号 and 迟到否=false)
from table0 a group by 班号,班级人数,迟到人数,未迟到人数