首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-09 
怎么统计表中的各个不同行内容的数目?比如有个表AA:IDType112232425162718291102怎么得到:TypeNum(Type的

怎么统计表中的各个不同行内容的数目?
比如有个表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

热点排行