我在下面有以下sql
SELECT
ttstudent.ttstudentid,
ttstudent.studentid,
ttstudent.subjectid,
ttstudent.classnumber,
ttstudent.classid,
concat(student.fn, " ", student.sn) AS Student,
SUM(If(ondemand.cycle="Feb7" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr7 Feb`,
SUM(If(ondemand.cycle="Jul7" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr7 July`,
SUM(If(ondemand.cycle="Feb8" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr8 Feb`,
SUM(If(ondemand.cycle="Jul8" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr8 July`,
SUM(If(ondemand.cycle="Feb9" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr9 Feb`,
SUM(If(ondemand.cycle="Jul9" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr9 July`,
SUM(If(ondemand.cycle="Feb10" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr10 Feb`,
SUM(If(ondemand.cycle="Jul10" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr10 Aug`,
ondemand.Student_ID
FROM ttstudent
INNER JOIN student ON ttstudent.studentid = student.code
INNER JOIN ondemand ON ttstudent.studentid = ondemand.Student_ID
GROUP BY ondemand.Student_ID
这为大约25个人生成一个柱状列表,最后一列是计算字段,用于查找表中最后两个值之间的差异。分数带有时间戳。
CODE |Year7Feb|Year7Jul|Year8Feb|Year8Jul|Year9Feb|Year9Jul| Year10Feb| Growth
abe1 | 2.3 | 2.9 | | | | | | .6
bas1 | | | 3.5 | 3.7 | | | | .2
cod | | | | | | 4.5 | 5.2 | .7
我想要做的是添加另一个列,该列将从每个用户(无论哪个列)中获取最后两个分数并找到差异。我将这个专栏称为增长。
我正在努力使用除了max以外的用途。有什么想法吗?
答案 0 :(得分:6)
这应该可以解决问题:
使用以下查询
计算列growth
SELECT si,ty,la.uid laid,pr.uid prid,(la.score-pr.score) growth FROM (
SELECT si,ty,max(test_date) cyprev, cylast FROM ondemand INNER JOIN (
SELECT Student_ID si,type ty,max(test_date) cylast FROM ondemand
GROUP BY Student_ID,type
) od ON si=Student_ID AND ty=type AND cylast>test_date
GROUP BY si,ty, cylast
) getlast2
INNER JOIN ondemand la ON la.Student_Id=si AND la.type=ty AND la.test_date=cylast
INNER JOIN ondemand pr ON pr.Student_Id=si AND pr.type=ty AND pr.test_date=cyprev
然后LEFT JOIN
它到您的整体查询(此处略微简化的版本):
SET @subj:="Numeracy";
SELECT Student_id,
SUM(If(ondemand.cycle="Feb7" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr7 Feb`,
SUM(If(ondemand.cycle="Jul7" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr7 July`,
SUM(If(ondemand.cycle="Feb8" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr8 Feb`,
SUM(If(ondemand.cycle="Jul8" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr8 July`,
SUM(If(ondemand.cycle="Feb9" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr9 Feb`,
SUM(If(ondemand.cycle="Jul9" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr9 July`,
SUM(If(ondemand.cycle="Feb10" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr10 Feb`,
SUM(If(ondemand.cycle="Jul10" and ondemand.type=@subj, ondemand.Score, NULL)) AS `Yr10 Aug`,
growth
FROM ondemand LEFT JOIN (
SELECT si,ty,la.uid laid,pr.uid prid,(la.score-pr.score) growth FROM (
SELECT si,ty,max(test_date) cyprev, cylast FROM ondemand INNER JOIN (
SELECT Student_ID si,type ty,max(test_date) cylast FROM ondemand
GROUP BY Student_ID,type
) od ON si=Student_ID AND ty=type AND cylast>test_date
GROUP BY si,ty, cylast
) getlast2
INNER JOIN ondemand la ON la.Student_Id=si AND la.type=ty AND la.test_date=cylast
INNER JOIN ondemand pr ON pr.Student_Id=si AND pr.type=ty AND pr.test_date=cyprev
) gt ON si=Student_id AND ty=@subj
GROUP BY Student_id;
我遗漏了JOIN
到表student
和ttstudent
及其列
ttstudent.ttstudentid,
ttstudent.studentid,
ttstudent.subjectid,
ttstudent.classnumber,
ttstudent.classid,
concat(student.fn, " ", student.sn) AS Student
修改强>
刚刚使用test_date
列进行了更改。子查询在MySQL中进行了测试,我希望它也适用于您的数据库。
<强> EDIT2:强>
我慢慢地看到你来自哪里。它变得越来越复杂(只是填写了关于type="Numeracy"
的xtra条件......也许毕竟有一个更简单的解决方案?
无论如何,这是一个 SQLfiddle 来演示整个事情(这里是修改后的版本:sqlfiddle2)。
第3次也是最后一次修改:
您想要想要的内容是SQLfiddle3( - &gt;仅一个 SELECT命令,没有前面的SET
语句)。您的完整命令应该如下所示:
SELECT
ttstudent.ttstudentid,
ttstudent.studentid,
ttstudent.subjectid,
ttstudent.classnumber,
ttstudent.classid,
concat(student.fn, " ", student.sn) AS Student,
SUM(If(ondemand.cycle="Feb7" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr7 Feb`,
SUM(If(ondemand.cycle="Jul7" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr7 July`,
SUM(If(ondemand.cycle="Feb8" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr8 Feb`,
SUM(If(ondemand.cycle="Jul8" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr8 July`,
SUM(If(ondemand.cycle="Feb9" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr9 Feb`,
SUM(If(ondemand.cycle="Jul9" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr9 July`,
SUM(If(ondemand.cycle="Feb10" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr10 Feb`,
SUM(If(ondemand.cycle="Jul10" and ondemand.type="Numeracy", ondemand.Score, NULL)) AS `Yr10 Aug`,
ondemand.Student_ID,
getdif.growth
FROM ttstudent
INNER JOIN student ON ttstudent.studentid = student.code
INNER JOIN ondemand ON ttstudent.studentid = ondemand.Student_ID
LEFT JOIN (
SELECT si,ty,la.uid laid,pr.uid prid,(la.score-pr.score) growth FROM (
SELECT si,ty,max(test_date) cyprev, cylast FROM ondemand INNER JOIN (
SELECT Student_ID si,type ty,max(test_date) cylast FROM ondemand
GROUP BY Student_ID,type
) od ON si=Student_ID AND ty=type AND cylast>test_date
GROUP BY si,ty, cylast
) getlast2
INNER JOIN ondemand la ON la.Student_ID=si AND la.type=ty AND la.test_date=cylast
INNER JOIN ondemand pr ON pr.Student_ID=si AND pr.type=ty AND pr.test_date=cyprev
) getdif ON si=ondemand.Student_ID AND ty=ondemand.type
WHERE ondemand.type='Numeracy'
GROUP BY ondemand.Student_ID
我之前的版本反映了我个人倾向于避免任何冗余,以及我尽可能多地参数化的野心。但我可能走得太远了; - )。