declare @t table
(id int,主题 varchar(20),已读 int,分类 int)insert @t
select 1,'ddd',1,1 union all
select 2,'cc',0,1 union all
select 3,'dfj',0,2 union all
select 4,'we',1,3select 分类,count(*) as 数量,sum(case when 已读=1 then 1 else 0 end) as 已读 from @t group by 分类分类 数量 已读
----------- ----------- -----------
1 2 1
2 1 0
3 1 1(所影响的行数为 3 行)
(id int,主题 varchar(20),已读 int,分类 int)insert @t
select 1,'ddd',1,1 union all
select 2,'cc',0,1 union all
select 3,'dfj',0,2 union all
select 4,'we',1,3select 分类,count(*) as 数量,sum(case when 已读=1 then 1 else 0 end) as 已读 from @t group by 分类分类 数量 已读
----------- ----------- -----------
1 2 1
2 1 0
3 1 1(所影响的行数为 3 行)
from 表
group by 分类
order by 分类
FROM 表
GROUP BY 分类
ORDER BY 分类