4

我在下面有以下 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 人生成一个列式列表,其中最后一列是计算字段,用于查找表中最后 2 个值之间的差异。分数带有时间戳。

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 之外的其他内容。有任何想法吗?

4

1 回答 1

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;

我省略了JOINstudentttstudent它们的列的 s

ttstudent.ttstudentid,
ttstudent.studentid,
ttstudent.subjectid,
ttstudent.classnumber,
ttstudent.classid,
concat(student.fn, " ", student.sn) AS Student

编辑:

Just made the changes using your test_date column. The subquery was tested in MySQL, I hope it also works in your database.

Edit2:

I slowly see where you are coming from. It's getting more and more complicated (just filled in the xtra conditions concerning type="Numeracy" ... Maybe there is an easier solution after all?

Anyway, here is a SQLfiddle to demonstrate the whole thing (and here a modified version: sqlfiddle2).

3rd and final edit:

What you probably want is something on the lines of SQLfiddle3 (--> only one SELECT command without a preceding SET statement). Your complete command should then look something like this:

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

My previous versions reflected my personal preference of avoiding any kind of redundancy and also my ambition to parameterize things as much as possible. But I probably went a bit too far ;-).

于 2013-08-03T11:57:14.433 回答