当前位置: 代码迷 >> Sql Server >> 50分,求 一 统计条数的 sql 语句!解决方法
  详细解决方案

50分,求 一 统计条数的 sql 语句!解决方法

热度:266   发布时间:2016-04-27 18:03:59.0
50分,求 一 统计条数的 sql 语句!
如题,有如下 数据

  姓名

  张三
  张三
  张三
  李四
  李四

想得到的结果是

  姓名 总数

  张三 3
  李四 2
 


------解决方案--------------------
SQL code
select 姓名 , count(*) 总数 from tb group by 姓名
------解决方案--------------------
SQL code
create table tb(姓名 varchar(10))insert into tb values('张三') insert into tb values('张三') insert into tb values('张三') insert into tb values('李四') insert into tb values('李四') GOselect 姓名 , count(*) 总数 from tb group by 姓名drop table tb/*姓名         总数          ---------- ----------- 李四         2张三         3(所影响的行数为 2 行)*/
------解决方案--------------------
SQL code
create table tb(姓名 varchar(10))insert into tb values('张三') insert into tb values('张三') insert into tb values('张三') insert into tb values('李四') insert into tb values('李四') GOselect distinct 姓名 , (select count(*) from tb where 姓名=a.姓名) 总数 from tb adrop table tb
------解决方案--------------------
上面都对的 帮顶
------解决方案--------------------
SQL code
create table tb(username varchar(10))insert into tb values('张三') insert into tb values('张三') insert into tb values('张三') insert into tb values('李四') insert into tb values('李四') GOselect username,count(*) 总数 from tb group by username
  相关解决方案