求个关于二表分组统计的SQL语句(在线等待)

bfdhh 2006-01-09 11:51:31
表1字段: 人员标识 人员 性别 国籍标识
1 a 男 1
2 b 女 2
3 c 男 1
4 d 女 1

表2字段: 国籍标识 国籍名称
1 中国
2 美国
3 韩国
返回表:

性别 中国 美国 韩国
男 2 0 0
女 1 1 0
合计 3 1 0

请大家帮忙,在线等待:
...全文
81 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lookatliu 2006-01-10
  • 打赏
  • 举报
回复
你这个一定要在sql文里面解决嘛?
bfdhh 2006-01-10
  • 打赏
  • 举报
回复
有个语句:
select a.* from
((select 1 as id,cSex
,count(case when gj='中国' then '' end) as 中国
,count(case when gj='美国' then '' end) as 美国
,count(case when gj='韩国' then '' end) as 韩国
,count(case when gj='意大利' then '' end) as 意大利
From
(select a.iHumanID,a.cHumanName,a.cSex,b.[Name] as gj From Human a left outer join gj b
on a.gjID=b.GJid) a group by cSex)

union
--求合计
(select 2 as id,'' as cSex,
count(case when gj='中国' then '' end) as 中国
,count(case when gj='美国' then '' end) as 美国
,count(case when gj='韩国' then '' end) as 韩国
,count(case when gj='意大利' then '' end) as 意大利
From
(select a.iHumanID,a.cHumanName,a.cSex,b.[Name] as gj From Human a left outer join gj b
on a.gjID=b.GJid) a)) a

这个语句只适合,在已知表2记录的情况下使用,

求个在不知表2记录时,使用的语句:
bfdhh 2006-01-09
  • 打赏
  • 举报
回复
数据库是sql server 2000

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧