这应该做的伎俩:
计算列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 [email protected], ondemand.Score, NULL)) AS `Yr7 Feb`,
SUM(If(ondemand.cycle="Jul7" and [email protected], ondemand.Score, NULL)) AS `Yr7 July`,
SUM(If(ondemand.cycle="Feb8" and [email protected], ondemand.Score, NULL)) AS `Yr8 Feb`,
SUM(If(ondemand.cycle="Jul8" and [email protected], ondemand.Score, NULL)) AS `Yr8 July`,
SUM(If(ondemand.cycle="Feb9" and [email protected], ondemand.Score, NULL)) AS `Yr9 Feb`,
SUM(If(ondemand.cycle="Jul9" and [email protected], ondemand.Score, NULL)) AS `Yr9 July`,
SUM(If(ondemand.cycle="Feb10" and [email protected], ondemand.Score, NULL)) AS `Yr10 Feb`,
SUM(If(ondemand.cycle="Jul10" and [email protected], 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 [email protected]
GROUP BY Student_id;
我忽略了JOIN
s到表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)
第三次和最后编辑:
你可能需要的是对SQLfiddle3东西线( - >只一个 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
我以前的版本中体现我的个人避免任何冗余的偏爱,也是我的志向参数化的东西尽可能。但是我可能走得太远了;-)。
你使用哪个数据库服务器,Mysql或SQL Server –
在'ondemand'中,你基本上拥有所有的信息。不幸的是,列“周期”不容易按日期排序。也许你应该考虑将该列重新格式化为日期时间或者使用像'2010-07'这样的字符串来表示周期。然后可以用有意义的方式对这些值进行排序。 – cars10m