2014-12-13 73 views
0

在MySql中,我需要在termid=10的一所學校的每個班級中選擇頂級學生,以獲得下一學期入學的折扣。Mysql最好的學生在學校的每一堂課

請注意,總不是表(我將在下面的清理問題)

我對所有學生該工作簿表工作簿:

id studentid classid exam1 exam2  total termid  
1  2   11  20  40  60  10   
2  1   22  40  20  60  10   
3  4   11  40  20  60  10   
4  5   33  10  60  70  10   
5  7   22  10  40  50  10   
6  8   11  10  30  40  10   
7  9   33  20  45  65  10   
8  11   11  null  null  null  10   
9  12   54  null  null  null  02 
10  13   58  null  null  null  02 

一號面臨的挑戰是:exam1exam2VARCHAR和總數不在表中(如我所解釋的)。

第二個挑戰是:可能是兩個學生具有頂級水平,使他們必須在結果是:你可以在id=8性病#11有沒有數字

3日挑戰是看到。

我需要的結果爲:

id studentid classid exam1 exam2  total termid  
1  2   11  20  40  60  10     
3  4   11  40  20  60  10   
4  5   33  10  60  70  10   
2  1   22  40  20  60  10 

我有這樣的查詢,但無法正常工作好,我提。

SELECT DISTINCT id,studentid,classid,exam1,exam2,total,termid ,(CAST(exam1 AS DECIMAL(9,2))+CAST(exam2 AS DECIMAL(9,2))) FROM workbook WHERE ClassId = '10'; 

回答

0

另一種方法是t o加入與自己的表。你找出最大爲每個類,然後加入匹配類最大,該類的所有學生:

最多爲每個類(包括在最後陳述時的話):

SELECT classid, MAX(CAST(exam1 AS UNSIGNED) + CAST(exam2 AS UNSIGNED)) as 'maxtotal' 
      FROM students 
      WHERE NOT ISNULL(exam1) 
       AND NOT ISNULL(exam2) 
      GROUP BY classid 

完整的語句:

SELECT s2.*, s1.maxtotal 
FROM (SELECT classid, MAX(CAST(exam1 AS UNSIGNED) + CAST(exam2 AS UNSIGNED)) as 'maxtotal' 
     FROM students 
     WHERE NOT ISNULL(exam1) 
      AND NOT ISNULL(exam2) 
     GROUP BY classid) s1 
JOIN students s2 ON s1.classid = s2.classid 
WHERE s1.maxtotal = (CAST(s2.exam1 AS UNSIGNED) + CAST(s2.exam2 AS UNSIGNED)); 

SQL小提琴:http://sqlfiddle.com/#!2/9f117/1

+0

該死的,打字速度太慢;-)戈登幹得好,如果價值爲零,合併也會返回零。 – Fuzzzzel 2014-12-13 11:00:23

+0

如果我想只有termid = '10'的結果,我必須要做什麼? – 2014-12-13 13:48:38

+0

Yhou必須在兩個WHERE子句中添加「AND termid = 10」/「AND s1.termid = 10」。通過這種方式,最大值僅根據第10項計算,也只有第10項的學生加入。 – Fuzzzzel 2014-12-13 14:54:32

0

由語句中使用簡單的組:

SELECT 
    studentid, 
    classid, 
    max(coalesce(exam1,0)) as max_exam_1, 
    max(coalesce(exam2,0)) as max_exam_2, 
    sum(coalesce(exam1,0) + coalesce(exam2,0)) as sum_exam_total, 
    termid 
FROM 
    workbook 
WHERE 
    termid=10 
GROUP BY 
    1,2 
ORDER BY 
    5 
2

你可以僅僅通過增加值(MySQL將這些值轉換爲數字)獲得總爲學生。下面獲取每一等級的最大總:

select w.classid, max(coalesce(w.exam1, 0) + coalesce(w.exam2, 0)) as maxtotal 
from workbook w 
group by w.classid; 

然後,您可以join這回的原始數據,以獲取有關最優秀的學生信息:

select w.*, coalesce(w.exam1, 0) + coalesce(w.exam2, 0) as total 
from workbook w join 
    (select w.classid, max(coalesce(w.exam1, 0) + coalesce(w.exam2, 0)) as maxtotal 
     from workbook w 
     group by w.classid 
    ) ww 
    on w.classid = ww.classid and (coalesce(w.exam1, 0) + coalesce(w.exam2, 0)) = ww.maxtotal; 
+0

它有一個錯誤:未知的表'w'。我真的嗎? – 2014-12-13 13:32:02

+0

@ms。 。 。這是一個失蹤的別名。它現在在那裏。 – 2014-12-13 13:41:56

0

嘗試是這樣的:

SELECT id,studentid,classid,exam1,exam2,(CAST(exam1 AS DECIMAL(9,2))+CAST(exam2 AS DECIMAL(9,2))) AS total,termid FROM `workbook` WHERE ((CAST(exam1 AS DECIMAL(9,2))+CAST(exam2 AS DECIMAL(9,2)))) > 50 

enter image description here

0

感謝所有的朋友

我想在上面2回答之間的結合是最好的:

SELECT s2.*, s1.maxtotal 
FROM (SELECT ClassId, MAX(
coalesce(exam1,0)+ 
coalesce(exam2,0) 
) as 'maxtotal' 
     FROM workbook 
     WHERE 
     (
termid = '11' 
    ) 
     GROUP BY ClassId) s1 
JOIN workbook s2 ON s1.ClassId = s2.ClassId 
WHERE s1.maxtotal = (
coalesce(exam1,0)+ 
coalesce(exam2,0) 
) AND (s1.maxtotal >'75'); 

最後一行是良好的s1.maxtotal = 0(有時學生的分數都沒有進入,所有等於0,所以都將顯示爲優秀學生)或有時我們需要最低分數(在下一學期註冊)。

非常感謝所有