日期:2014-05-18  浏览次数:20376 次

sql查询问题
有一个表如下
姓名 男 女 籍贯
雷超 1 0 洛阳
雷超 0 1 郑州
杨泓 1 0 兰州
杨泓 0 1 邯郸
杨泓 1 0 南通

现在要统计全国各种姓名性别分别是男女的人数,比如全国叫杨泓的男的多少,女的多少,1表示是,0表示不是,怎样将这个表用SQL语句查询成为如下的表
姓名 男性人数 女性人数
雷超 1 1
杨泓 2 1
在线等!!!

------解决方案--------------------
SQL code

--测试数据
create table Class(Stu_Name nvarchar(10),man bit,woman bit,Stu_city nvarchar(10))

insert into Class
select '雷超',1,0,'洛阳'
union select '雷超',0,1,'郑州'
union select '杨泓',1,0,'兰州'
union select '杨泓',0,1,'邯郸'
union select '杨泓',1,0,'南通'

--查找语句
select Stu_Name as '姓名'
,sum(case man when 1 then 1 else 0 end) as '男性人数'
,sum(case woman when 1 then 1 else 0 end) as '女性人数'
from dbo.Class 
group by Stu_Name

------解决方案--------------------
探讨

SQL code

--测试数据
create table Class(Stu_Name nvarchar(10),man bit,woman bit,Stu_city nvarchar(10))

insert into Class
select '雷超',1,0,'洛阳'
union select '雷超',0,1,'郑州'
union select '杨泓',1,0,'兰州'
union……