GROUP BY是对某(些)字段进行合计统计的,而不是对某(些)字段的某个值进行统计,问题就在这儿!如果用程序逻辑序列当然可以满足用户的这些要求,但是我希望能用一条SQL语句就可以满足用户的要求。
select 年级,班级,专业,sum(政治面貌) group by 政治面貌 上面的SQL语句当然不行,且根本不能通过PARSE,因为GROUP BY 一定要用到SELECT字段中的所有非合计字段。SELECT 年级, 班级, 专业, SUM(政治面貌) GROUP BY 年级, 班级, 专业 这条SQL统计的结果不满足用户在上面提到的要求啊。
select 年级,班级,专业, sum(if 政治面貌 = ‘党员’then 1 else 0) as 党员, sum(if 政治面貌 = ‘团员’then 1 else 0) as 团员, sum(if 政治面貌 = ‘群众’then 1 else 0) as 群众 group by 年级,班级,专业 order by 年级,班级,专业
创建三张视图 create view dang(grade,class,number,identify) as select 年及,班及,学号,政志面貌 from stufenttable where domain='党员‘; create view tuan(………………)…………………………………………………… where domain='团员’; create view qun() where domain=' ' 你再从三张视图中选 ,关连dang.class=tuan.class=qun.class 总数用count(number) group by class 就可以了
select 年级,班级,专业, sum(if 政治面貌 = ‘党员’then 1 else 0) as 党员, sum(if 政治面貌 = ‘团员’then 1 else 0) as 团员, sum(if 政治面貌 = ‘群众’then 1 else 0) as 群众 from 学生表 group by 年级,班级,专业 order by 年级,班级,专业 不好用?
楼上的兄弟,不行啊!下面是在MS SQL Server 7.0中运行后的错误提示 Incorrect syntax near the keyword 'if'.
用这个嵌套的查询: select 年级,班级,专业, 党员=(select count(*) From Table where 政治面貌 ='党员' and 年级=b.年级 and 班级=b.班级 and 专业=b.专业), 团员=(select count(*) From Table where 政治面貌 ='团员' and 年级=b.年级 and 班级=b.班级 and 专业=b.专业), 群众=(select count(*) From Table where 政治面貌 ='群众' and 年级=b.年级 and 班级=b.班级 and 专业=b.专业), from Table1 a group by 年级,班级,专业 ================================================================ 一颗红心向前看,为了革命两茫然,不好意思才囊尽,只能说上一点点。 ★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆ ★ ★ ★ ★ ★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆ ================================================================
我告诉你的是方法,你得朝这个方向想 自己去构思脚本 select 年级,班级,专业, sum(case when 政治面貌 = '党员'then 1 else 0 end) as 党员, sum(case when 政治面貌 = '团员’then 1 else 0 end) as 团员, sum(case when 政治面貌 = '群众'then 1 else 0 end) as 群众 group by 年级,班级,专业 order by 年级,班级,专业 如果行就给分
上面的SQL语句当然不行,且根本不能通过PARSE,因为GROUP BY 一定要用到SELECT字段中的所有非合计字段。SELECT 年级, 班级, 专业, SUM(政治面貌) GROUP BY 年级, 班级, 专业
这条SQL统计的结果不满足用户在上面提到的要求啊。
sum(if 政治面貌 = ‘党员’then 1 else 0) as 党员,
sum(if 政治面貌 = ‘团员’then 1 else 0) as 团员,
sum(if 政治面貌 = ‘群众’then 1 else 0) as 群众
group by 年级,班级,专业
order by 年级,班级,专业
create view dang(grade,class,number,identify) as select 年及,班及,学号,政志面貌
from stufenttable where domain='党员‘;
create view tuan(………………)……………………………………………………
where domain='团员’;
create view qun() where domain=' '
你再从三张视图中选 ,关连dang.class=tuan.class=qun.class
总数用count(number) group by class 就可以了
sum(if 政治面貌 = ‘党员’then 1 else 0) as 党员,
sum(if 政治面貌 = ‘团员’then 1 else 0) as 团员,
sum(if 政治面貌 = ‘群众’then 1 else 0) as 群众
from 学生表
group by 年级,班级,专业
order by 年级,班级,专业
不好用?
Incorrect syntax near the keyword 'if'.
不过暂时还不想结贴,我想知道在SQLSERVER中有没有相当的方法?
不过暂时还不想结贴,我想知道在SQLSERVER中有没有相当的方法?
解决用户上面的需要,而解决端不在应用程序,而在数据库这一端。
呵呵。
happyzyx(快乐的小能)的方法在ACCESS中已经解决掉了!我试过。
select 年级,班级,专业,
党员=(select count(*) From Table where 政治面貌 ='党员' and 年级=b.年级 and 班级=b.班级 and 专业=b.专业),
团员=(select count(*) From Table where 政治面貌 ='团员' and 年级=b.年级 and 班级=b.班级 and 专业=b.专业),
群众=(select count(*) From Table where 政治面貌 ='群众' and 年级=b.年级 and 班级=b.班级 and 专业=b.专业),
from Table1 a
group by 年级,班级,专业
================================================================
一颗红心向前看,为了革命两茫然,不好意思才囊尽,只能说上一点点。
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
★ ★
★ ★
★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
================================================================
自己去构思脚本
select 年级,班级,专业,
sum(case when 政治面貌 = '党员'then 1 else 0 end) as 党员,
sum(case when 政治面貌 = '团员’then 1 else 0 end) as 团员,
sum(case when 政治面貌 = '群众'then 1 else 0 end) as 群众
group by 年级,班级,专业
order by 年级,班级,专业
如果行就给分