2013-08-03 85 views
4

我有以下查找表中作爲列兩個領域之間的區別

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之差計算字段下面的SQL表中的值。得分是時間戳。

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 

我想這樣做是另一列添加這將需要從每個用戶(無論列它是)最後兩個得分並找到差異。我會稱這一列增長。

我正在努力使用以外的最大值。有任何想法嗎?

+0

你使用哪個數據庫服務器,Mysql或SQL Server –

+1

在'ondemand'中,你基本上擁有所有的信息。不幸的是,列「週期」不容易按日期排序。也許你應該考慮將該列重新格式化爲日期時間或者使用像'2010-07'這樣的字符串來表示週期。然後可以用有意義的方式對這些值進行排序。 – cars10m

回答

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 [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到表studentttstudent及其列

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 

我以前的版本中體現我的個人避免任何冗餘的偏愛,也是我的志向參數化的東西儘可能。但是我可能走得太遠了;-)。

+0

使用ondemand中的字段打上時間戳,名爲test_date,格式爲「2013-02-06 11:52:32」它是MYSQL服務器,我使用的是Navicat感謝 – user2647527

+0

這不起作用爲我工作我收到錯誤或何時在開始時使用SET命令獲取mysql語法錯誤。 – user2647527

+0

我假設你使用類似'mysqli_query()'或'pdo-> query()'的方法從PHP發送SQL命令。無論如何,每次調用只能發送一個** SQL命令,因此您必須將上述SQL命令分成**兩個** PHP函數調用,首先是「SET ...」,然後是大'SELECT'命令。由於它們都是從同一個PHP文件發送的,因此定義的SQL變量不會在兩者之間失去作用域。 – cars10m