{好急}一个数据汇总问题(50分)

  • 主题发起人 主题发起人 xx6620063
  • 开始时间 开始时间
X

xx6620063

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个表是这样:
学 校 姓 名 性别 民族 年 级 班名
新民小学 陈东 男 苗族 一年级 1班
下村小学 陈好 女 汉族 一年级 2班
新民小学 小华 男 汉族 一年级 3班
新民小学 平华 女 苗族 一年级 1班
下村小学 小英 男 汉族 六年级 1班

几千条记录
==================================================
用SQL生在一个查询汇总:
学 校 一年级(总数) 一年级(女生数) 一年级(少数民族数) ......六年级(少..
新民小学 3 1 2
下村小学 1 1 0
==========================================================================
请高手帮一下!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
高手们出来吧!!!!!!!!!!!!!!
 
select 学校,
(select count(*) from 学校表 where 年级='一年级' and 学校=b.学校) as 一年级总数,
(select count(*) from 学校表 where 年级='一年级' and 性别='女' and 学校=b.学校) as 一年级女生,
(select count(*) from 学校表 where 年级='一年级' and 民族<>'汉族' and 学校=b.学校) as 一年级少数民族
......
......
from 学校表 b
group by 学校
 
select 学校,count(一年级总数),count(一年级女生数),count(一年级少数民族数),count(二年级总数),count(二年级女生数),count(二年级少数民族数)...count(六年级少数民族数) from
(select 学校,count(*) as 一年级总数,case 性别 when 女 then 1 else 0 end ,case 民族 when 汉族 then 0 else 1 end ,0 as 二年级总数,0 as 二年级女生数,0 as 二年级少数民族数....0 as 六年级少数民族数 where 年级='一年级'
union all
...二年级的
union all
...三年级的
union all
...四年级的
union all
...五年级的
union all
...六年级的

) a grup by 学校
 
TO:liyoumin 我试了不能运行,有没有更好的方法
 
select distinct 学校,
(select count(*) from 学校表 where 年级='一年级' and 学校=b.学校) as 一年级总数,
(select count(*) from 学校表 where 年级='一年级' and 性别='女' and 学校=b.学校) as 一年级女生,
(select count(*) from 学校表 where 年级='一年级' and 民族<>'汉族' and 学校=b.学校) as 一年级少数民族
......
......
from 学校表 b
呵呵,我是菜鸟,试验一下
 
select 学校,
count(case when where 年级='一年级'then 1 else 0 end) as 一年纪总数
count(case when where 年级='一年级' 性别='女' and then 1 else 0 end) as 一年纪女生,
。。。
group by 学校


就是上面的格式,sqlserver肯定可以。
 
哪位高手来帮一下,感谢了
 
TO:hityou
应该是这样吧
select 学校,
sum(case when where 年级='一年级'then 1 else 0 end) as 一年纪总数
sum(case when where 年级='一年级' 性别='女' and then 1 else 0 end) as 一年纪女生,
。。。
group by 学校


就是上面的格式,sqlserver肯定可以。
 
我做的一个报表就是用这个方法统计的

呵呵,第二个sum那里应该是
select 学校,
sum(case when 年级='一年级'then 1 else 0 end) as 一年纪总数
sum(case when 年级='一年级'and 性别='女' and then 1 else 0 end) as 一年纪女生
group by 学校

楼主先用这个试试,然后别的都是类似,不难。
 
高手们,我用是ACCESS,所以不能运行,你们看有什么办法
 
ACCESS好象没有CASE,
 
是不是表建的就不合理呢
 
早不说

SELECT school,
count(name),
count(iif(grade='一',1,0)) as 一年纪,
count(iif((grade='一' and sex='男'),1,0)) as 一年纪男
FROM test5 group by school

用iif就可以了。
 
ft,用sum就对了

SELECT school,
count(name),
sum(iif(grade='一',1,0)) as 一年纪,
sum(iif((grade='一' and sex='男'),1,0)) as 一年纪男
FROM test5 group by school

楼主按照这个来吧。
 
把我写的CASE替换为IIF行吗
 
谢谢大家了 下次问题请hityou帮忙
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部