這應該做的伎倆:
計算列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