关于数据库统计查询的sql语句
2005-07-29 12:36
981 查看
在 网上看到了很多提问关于统计查询sql的写法, 其实问题的本质都一样, 能解决一个,就能解决所有的(我大胆的这样想.估计不完全正确). 这些问题的描述如下:
有这样一个表:
StudetNum
我对这个表作简单的介绍: 这个表存储学生的成绩. id是这个表的关键字段, studetno(我知道student是这样写的, 嘿嘿) 代表学生的学号, class是课程. numeric是成绩.
当然这个表是不够合理的, 暂且不讨论这个问题.等我下期再研究这个问题.
如果要得到下面的查询结果 ,该怎么作呢?
tongsuchaxun
目前大家可以清楚的看到这张学生课程成绩表中课程只有3种, 假设在应用程序中学生的课程不变, 那么如下sql将很简单的解决了问题:
select studetno, (select numeric from studetnum t1 where class= 'Yw' and t.studetno=t1.studetno) as Yw,
(select numeric from studetnum t2 where class= 'English' and t.studetno=t2.studetno) as English,
(select numeric from studetnum t3 where class= 'SHUXUE' and t.studetno=t3.studetno) as SHUXUE,
(select sum(numeric) from studetnum t4 where t.studetno=t4.studetno) as Total,
(select avg(numeric) from studetnum t5 where t.studetno=t5.studetno) as Avg from StudetNum t group by studetno
还有一种方法是把select子查询改成 case when 子句。
但是如果学生课程是可变的, 我需要得到这样的查询表格:
studetno class1 class2 class3 .... classN sum avg
1 68 96 80 .... 87 * *
.....................
.....................
那么该如何写一条具有伸缩弹性的sql语句呢?? 一条不行?? 那么就用存储过程,
可是假如数据库是access 的,你还会用存储过程嘛? 当然,你不会说一定要在程序逻辑中实现吧?
有这样一个表:
ID | StudetNo | Class | numeric |
---|---|---|---|
1 | 1 | Yw | 80 |
2 | 1 | SHUXUE | 90 |
3 | 1 | English | 60 |
4 | 2 | Yw | 60 |
5 | 2 | SHUXUE | 30 |
6 | 2 | English | 60 |
7 | 3 | Yw | 80 |
8 | 3 | SHUXUE | 40 |
9 | 3 | English | 40 |
10 | 4 | Yw | 20 |
11 | 4 | SHUXUE | 140 |
12 | 5 | English | 60 |
13 | 6 | English | 50 |
14 | 7 | Yw | 40 |
当然这个表是不够合理的, 暂且不讨论这个问题.等我下期再研究这个问题.
如果要得到下面的查询结果 ,该怎么作呢?
studetno | Yw | English | SHUXUE | Total | Avg |
---|---|---|---|---|---|
1 | 80 | 60 | 90 | 230 | 76.6666666666667 |
2 | 60 | 60 | 30 | 150 | 50 |
3 | 80 | 40 | 40 | 160 | 53.3333333333333 |
4 | 20 | 140 | 160 | 80 | |
5 | 60 | 60 | 60 | ||
6 | 50 | 50 | 50 | ||
7 | 40 | 40 | 40 |
select studetno, (select numeric from studetnum t1 where class= 'Yw' and t.studetno=t1.studetno) as Yw,
(select numeric from studetnum t2 where class= 'English' and t.studetno=t2.studetno) as English,
(select numeric from studetnum t3 where class= 'SHUXUE' and t.studetno=t3.studetno) as SHUXUE,
(select sum(numeric) from studetnum t4 where t.studetno=t4.studetno) as Total,
(select avg(numeric) from studetnum t5 where t.studetno=t5.studetno) as Avg from StudetNum t group by studetno
还有一种方法是把select子查询改成 case when 子句。
但是如果学生课程是可变的, 我需要得到这样的查询表格:
studetno class1 class2 class3 .... classN sum avg
1 68 96 80 .... 87 * *
.....................
.....................
那么该如何写一条具有伸缩弹性的sql语句呢?? 一条不行?? 那么就用存储过程,
可是假如数据库是access 的,你还会用存储过程嘛? 当然,你不会说一定要在程序逻辑中实现吧?
相关文章推荐
- 统计一个数据库中表个数的sql查询语句
- 关于数据库查询“行转列”的sql语句
- 关于数据库查询语句SqlDataReader的连接释放问题的解决办法
- 关于过去工作的一些总结:常用sql语句以及数据库查询优化
- 本人开发系统--新生报到与数据统计管理系统--数据库设计及相关查询SQL语句笔记
- 关于数据库中的简单SQL查询语句
- 数据库sql查询语句备忘
- 关于同时查询父子名称的SQL查询语句的写法 id name parentId parentName .
- Oracle性能-查询统计信息的SQL语句
- 关于一些常用SQL设置数据库的语句
- orcale数据库基本查询语句(SQL)
- ACCESS:跨数据库查询的SQL语句
- ACCESS:跨数据库查询的SQL语句
- 营配数据质量核查,关于营销mis系统与配电gis系统里面的sql语句查询,做为积累使用,下次就不用重复写同样的语句了。
- SQL 统计查询语句消耗时间
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- 大数据量查询优化——数据库设计、SQL语句、JAVA编码
- 关于同时查询父子名称的SQL查询语句的写法 id name parentId parentName
- .NET C# 将 mdb 中数据读为 list<string[]> 其中 path 为数据库地址 ,sql 为查询语句
- 数据库多表查询SQL语句