读书人

如何统计表中的各个不同行内容的数目

发布时间: 2012-04-09 13:41:25 作者: rapoo

怎么统计表中的各个不同行内容的数目?
比如有个表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 allselect 2,2 union allselect 3,2 union allselect 4,2 union allselect 5,1 union allselect 6,2 union allselect 7,1 union allselect 8,2 union allselect 9,1 union allselect 10,2select [Type],COUNT(distinct [ID]) as counts from AA group by [Type]/*Type    counts1    42    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

读书人网 >SQL Server

热点推荐