日期:2014-05-18  浏览次数:20506 次

求分组后,数据条数
select a
from t
group by a

求查询出来的记录条数

------解决方案--------------------
SQL code
select a,count(*)条数
from t
group by a

------解决方案--------------------
SQL code

declare @t table (a int)
insert into @t
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 3

--分组后的结果 
select a from @t group by a
/*
a
-----------
1
2
3
*/

--每组的条数
select a,count(1) as cnt from @t group by a
/*
a           cnt
----------- -----------
1           2
2           2
3           1
*/

--总条数
select count(distinct a) as cnt from @t
/*
cnt
-----------
3
*/