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

怎么统计表中的各个不同行内容的数目?
比如有个表AA:

ID Type
1 1
2 2
3 2
4 2
5 1
6 2
7 1
8 2
9 1
10 2

怎么得到:

Type Num(Type的数量)
1 4
2 6


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

if object_id('[AA]') is not null drop table [AA]
create table [AA]([ID] int,[Type] int)
insert [AA]
select 1,1 union all
select 2,2 union all
select 3,2 union all
select 4,2 union all
select 5,1 union all
select 6,2 union all
select 7,1 union all
select 8,2 union all
select 9,1 union all
select 10,2

select [Type],COUNT(distinct [ID]) as counts from AA group by [Type]

/*
Type    counts
1    4
2    6
*/

------解决方案--------------------
select type , count(*) num from aa group by type
select type , count(type) num from aa group by type
select type , count(1) num from aa group by type