比如有个表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