日期:2014-05-19  浏览次数:20495 次

高分求解 MS SQL语句,在线等待。
表结构如下:

ID           USER       SEX         AGE
1             aa             男           22
2             bb             女           28
3             aa             男           22
4             bb             女           55
5             cc             女           33
6             bb             男           44
7             aa             男           22
8             cc             女           12
9             bb             男           88
10           aa             男           22

我想做个统计,统计出   user   这一列中   相同名字的个数,比如:以上表为例最后的结果是:   叫aa的有4     叫bb的有4个   叫cc的有2个。
目前,表中只知道列名,不知道列中的具体值,请高手帮忙,这样的语句怎么写呀。谢谢了。。在线等待

------解决方案--------------------
select user,count(1) as 个数
from t
group by user

------解决方案--------------------
select user,count(*) from tablename group by user
--if you want count(*)> 1
--then plus: having count(*)> 1
------解决方案--------------------
--创建测试环境
create table t(ID int,[USER] varchar(10),SEX varchar(2),AGE int)

--插入测试数据
insert t(ID,[USER],SEX,AGE)
select '1 ', 'aa ', '男 ', '22 ' union all
select '2 ', 'bb ', '女 ', '28 ' union all
select '3 ', 'aa ', '男 ', '22 ' union all
select '4 ', 'bb ', '女 ', '55 ' union all
select '5 ', 'cc ', '女 ', '33 ' union all
select '6 ', 'bb ', '男 ', '44 ' union all
select '7 ', 'aa ', '男 ', '22 ' union all
select '8 ', 'cc ', '女 ', '12 ' union all
select '9 ', 'bb ', '男 ', '88 ' union all
select '10 ', 'aa ', '男 ', '22 '

--求解过程
select [user],count(1) as 个数
from t
group by [user]


--删除测试环境
drop table t

/*--测试结果
user 个数
---------- -----------
aa 4
bb 4
cc 2

(所影响的行数为 3 行)
*/



------解决方案--------------------
select user,count(*)
from 表名
group by user
------解决方案--------------------
select user,count(*) as 个数
from table
group by user
------解决方案--------------------