请教 这样的SQL该如何写?(100分)

  • 主题发起人 主题发起人 sxbug
  • 开始时间 开始时间
S

sxbug

Unregistered / Unconfirmed
GUEST, unregistred user!
请教 这样的SQL该如何写?

我的库结构是这样的:
学生库:
学号 姓名 班级 年龄 专业 系 家庭住址
--------------------------------------------------
01 张三 9901 20 电器 电子 湖南
02 立嗣 9901 19 电器 电子 湖北

成绩库:
学号 学期 年级 课程号 成绩 班级排名 年级排名
--------------------------------------------------
01 1 2 003 86 5 10
01 1 2 006 90 2 7
01 1 2 010 81 9 20

课程库:
课程号 课程名称 任课老师 专业
------------------------------------
001 语文 张力 文学
002 数学 8888 文学
003 地理 3333 444
004 历史 333 33
005
006


我现在想用query连接这些表,在dbgrid里面显示:

学号 姓名 课程名 成绩 课程名 成绩 课程名 成绩 总分 平均分 排名
------------------------------------------------------------------------

课程数目由学生本学期 开课数目 决定。
总分 和 平均分 也作为2门课程类似处理,入库。

望高人指点!!提供些思路也可以。
 
这样的显示结构似乎是有欠合理哦!
而且这里的排名到底是对应的课程的班级,年级排名
还是总分的班级,年级排名啊?
 
什么数据库? 如果是Access、Excel,可以很方便的利用它们内置的“交叉表”功能实现,
如果是MSSQL、Oracle等,就要比较复杂的算法了。
Access的做法如下:
首先建立一个查询v_cj:
select a.学号,a.姓名,c.课程名,b.成绩 from 学生库 a,成绩库 b,课程库 c
where a.学号=b.学号 and b.课程号=c.课程号 and ...(学习条件)
然后建立一个交叉表v_crosscj,得到这么一个表:
(具体忘了,用向导)
学号 姓名 课程名 成绩 课程名 成绩 课程名 成绩

Delphi中设立数据源连接到v_crosscj,总分、平均分、排名用计算字段,即可。
 
而且如果两个学生所选的课程数目不同,要如何处理?
 
Query1:
select 学号,sum(成绩) as 总分,avg(成绩 ) as 平均分
from 课程库
group by 学号
Query2
select 学号 ,姓名 , 课程名 , 成绩 , 课程名 , 成绩 ,
课程名 , 成绩 , 班级排名 , 年级排名
from 学生库 inner join 成绩库 on 学生库.学号=成绩库.学号
inner join 课程库 on 成绩库.课程号=课程库.课程号
在Query2中建立关联Query的查询字段 总分 平均分

 
select 学生库.学号 ,学生库.姓名,课程库. 课程名,成绩库.成绩,成绩库.排名
from 学生库,课程库,成绩库
where 成绩库.学号 =学生库.学号 and 成绩库.课程号 =课程库.课程号
 
以下语句会对你有帮助

Assume there is a table Pivot that has one row per quarter. A SELECT of Pivot reports the quarters vertically:

Year Quarter Amount

---- ------- ------

1990 1 1.1

1990 2 1.2

1990 3 1.3

1990 4 1.4

1991 1 2.1

1991 2 2.2

1991 3 2.3

1991 4 2.4



A report must be produced with a table that contains one row for each year, with the values for each quarter appearing in a separate column, such as:

Year Q1 Q2 Q3 Q4
1990 1.1 1.2 1.3 1.4
1991 2.1 2.2 2.3 2.4



These are the statements to create the Pivot table and populate it with the data from the first table:

USE Northwind

GO



CREATE TABLE Pivot

( Year SMALLINT,

Quarter TINYINT,

Amount DECIMAL(2,1) )

GO

INSERT INTO Pivot VALUES (1990, 1, 1.1)

INSERT INTO Pivot VALUES (1990, 2, 1.2)

INSERT INTO Pivot VALUES (1990, 3, 1.3)

INSERT INTO Pivot VALUES (1990, 4, 1.4)

INSERT INTO Pivot VALUES (1991, 1, 2.1)

INSERT INTO Pivot VALUES (1991, 2, 2.2)

INSERT INTO Pivot VALUES (1991, 3, 2.3)

INSERT INTO Pivot VALUES (1991, 4, 2.4)

GO



This is the SELECT statement to create the rotated results:

SELECT Year,

SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,

SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,

SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,

SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4

FROM Northwind.dbo.Pivot

GROUP BY Year

GO



This SELECT statement also handles a table in which there are multiple rows for each quarter. The GROUP BY combines all rows in Pivot for a given year into a single row in the output. When the grouping operation is being performed, the CASE functions in the SUM aggregates are applied in such a way that the Amount values for each quarter are added into the proper column in the result set and 0 is added to the result set columns for the other quarters.

If the results of this SELECT statement are used as input to a spreadsheet, it is easy for the spreadsheet to calculate a total for each year. When the SELECT is used from an application it may be easier to enhance the SELECT statement to calculate the yearly total, for example:

SELECT P1.*, (P1.Q1 + P1.Q2 + P1.Q3 + P1.Q4) AS YearTotal

FROM (SELECT Year,

SUM(CASE P.Quarter WHEN 1 THEN P.Amount ELSE 0 END) AS Q1,

SUM(CASE P.Quarter WHEN 2 THEN P.Amount ELSE 0 END) AS Q2,

SUM(CASE P.Quarter WHEN 3 THEN P.Amount ELSE 0 END) AS Q3,

SUM(CASE P.Quarter WHEN 4 THEN P.Amount ELSE 0 END) AS Q4

FROM Pivot AS P

GROUP BY P.Year) AS P1
 
如果课程数不同,就取最多的
排名 在生成的dbgrid中最好就按总分来排
 
在成绩库中的排名 按单科成绩。
顺便问一下,就是使用成绩库和学生库,怎样填写单科成绩排名(就是计算排名了)
 
后退
顶部