我有一个类的作业,要求我在SQL中编写一个程序,该程序获取原始硬件和测试分数,然后计算每个作业的百分比以及总加权平均值。表中的主键是学生的SSN。每个作业的最高分数存储在SSN =' 0001'并且分配的权重存储在SSN =' 0002下。'我得到一个"子查询返回多行"错误,我不知道我哪里出错了。如果有人可以帮助我,我会非常感激。
SET hw1M = (SELECT hw1 FROM RAW_SCORES WHERE (SSN = '0001'));
SET hw1W = (SELECT hw1 FROM RAW_SCORES WHERE (SSN = '0002'));
SET hw2aM = (SELECT hw2a FROM RAW_SCORES WHERE (SSN = '0001'));
SET hw2aW = (SELECT hw2a FROM RAW_SCORES WHERE (SSN = '0002'));
SET hw2bM = (SELECT hw2b FROM RAW_SCORES WHERE (SSN = '0001'));
SET hw2bW = (SELECT hw2b FROM RAW_SCORES WHERE (SSN = '0002'));
SET MidtermM = (SELECT Midterm FROM RAW_SCORES WHERE (SSN = '0001'));
SET MidtermW = (SELECT Midterm FROM RAW_SCORES WHERE (SSN = '0002'));
SET hw3M = (SELECT HW3 FROM RAW_SCORES WHERE (SSN = '0001'));
SET hw3W = (SELECT HW3 FROM RAW_SCORES WHERE (SSN = '0002'));
SET FExamM = (SELECT FExam FROM RAW_SCORES WHERE (SSN = '0001'));
SET FExamW = (SELECT FExam FROM RAW_SCORES WHERE (SSN = '0002'));
SET HW1Pct = ((SELECT Hw1 FROM RAW_SCORES)/hw1M);
SET HW2aPct = ((SELECT Hw2a FROM RAW_SCORES)/hw2aM);
SET HW2bPct = ((SELECT Hw2b FROM RAW_SCORES)/hw2bM);
SET MidtermPct = ((SELECT Midterm FROM RAW_SCORES)/MidtermM);
SET HW3Pct = ((SELECT Hw3 FROM RAW_SCORES)/hw3M);
SET FExamPct = ((SELECT FExam FROM RAW_SCORES)/FExamM);
SET WeightedAverage = ((HW1Pct * hw1w) + (HW2Pct * hw2aw) + (HW2bPct * hw2bw) + (MidtermPct * Midtermw) + (HW3Pct * hw3w) + (FExamPct * FExamw));
SELECT SSN, FName, LName, HW1Pct, HW2Pct, MidtermPct, HW3Pct, FExamPct FROM RAW_SCORES;
答案 0 :(得分:0)
这样想 -
所有学生
SELECT *
FROM RAW_SCORES
WHERE SSN NOT IN ('0002','0001')
HW1的AVG评分
SELECT AGV(HW1)
FROM RAW_SCORES
WHERE SSN NOT IN ('0002','0001')
所有学生的平均百分比
SELECT SSN, HW1 / X.AVG_HW1 AS PERCENT_OF_AVG_HW1
FROM RAW_SCORES
CROSS JOIN (
SELECT AGV(HW1) AS AVG_HW1
FROM RAW_SCORES
WHERE SSN NOT IN ('0002','0001')
) AS X
WHERE SSN NOT IN ('0002','0001')
等
我知道这不是你教授所要求的 - 我的观点不是为你做功课,而是帮助你理解如何使用集合来解决SQL中的问题,而不是像编程控件一样导向语言。所以我用这种方式给你一个表达式的例子,并假设你可以将它扩展到所需的表达式。