sql 查询科目成绩以及平均成绩
2016-10-08 18:49
344 查看
score表
stuid subject score
1 math 80
1 english 90
2 math 81
2 english 91
3 math 85
3 english 95
要求得到的组合查询结果
id math english sum
1 80 90 170
2 81 91 172
3 85 95 180
avg 82 92 174
union 对两个结果集进行并集操作,重复数据只显示一次
union All,对两个结果集进行并集操作,重复数据全部显示
stuid subject score
1 math 80
1 english 90
2 math 81
2 english 91
3 math 85
3 english 95
要求得到的组合查询结果
id math english sum
1 80 90 170
2 81 91 172
3 85 95 180
avg 82 92 174
( SELECT stuid as id, CAST(sum(case when `subject`='math' then score end) AS SIGNED) as math, CAST(sum(case when `subject`='english' then score end) AS SIGNED) as english, CAST(( sum(case when `subject`='math' then score end) + sum(case when `subject`='english' then score end) ) AS SIGNED) as 'sum' FROM score GROUP BY stuid ) UNION ( SELECT 'avg' as id, CAST(avg(case when `subject`='math' then score end) AS SIGNED) as math, CAST(avg(case when `subject`='english' then score end) AS SIGNED) as english, CAST(( avg(case when `subject`='math' then score end) + avg(case when `subject`='english' then score end) ) AS SIGNED) as 'sum' FROM score )
union 对两个结果集进行并集操作,重复数据只显示一次
union All,对两个结果集进行并集操作,重复数据全部显示
相关文章推荐
- 只有两个字段用一个sql语句查询出某个学生的姓名、成绩以及在表中的排名
- 请用一句sql语句取出各科的平均成绩,显示字段,科目,平均成绩
- SQL查询每门科目的成绩大于80的学生名字
- sql 建表以及查询---复杂查询之成绩排名
- sql同时查询文理科平均成绩
- SQL的老题目:查询学生平均成绩及其名次
- SQL语句执行顺序以及oracle基本查询优化
- NHibernate 操作原生SQL以及查询DataTable,DataSet
- 一个student表有姓名和成绩两个字段。查询某个学生的姓名和成绩以及排名。
- [转自JeffreyZhao]在LINQ to SQL中使用Translate方法以及修改查询用SQL
- 成绩查询sql
- 有一个班,3个学生,各4门课,计算总平均分数以及第n个学生的成绩
- Nhibernate操作原生SQL以及查询DataTable
- [转自JeffreyZhao]在LINQ to SQL中使用Translate方法以及修改查询用SQL
- T_Sql 交叉连接以及查询等等 暑假第二天
- T_Sql 交叉连接以及查询等等 暑假第二天
- 查询成绩>=该课程平均成绩的信息,并按格式输出
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL (转)
- SQL查询每所学校语文成绩最高的学生信息
- 在LINQ to SQL中使用Translate方法以及修改查询用SQL 推荐