2016-01-24 55 views
0

我將更好地描述我的問題,並且希望包含我的數據。SQL MAX語句

我有我需要從表6的數據:

學生小號 StoredGrades SG t檢驗 TestScores TS StudentTest ST StudentTestScore STS

這裏就是我想要做的。我需要一份可以運行的報告,告訴我最近一個學期最近一個學期獲得了什麼成績,以及他們在HS生涯中的最佳PSAT考試成績。

學生(S)

ID Student_Number lastfirst grade_level 
1 1 Bird, Big 9 
2 2 Bob, Sponge 10 
3 3 Man, He 9 
4 4 Rah, She 10 
5 5 Myers, Michael 11 

StoredGrades SG (我需要收集的課程名稱和編號以及老師的名字和他們所收到的等級)

course_name Course_number Grade Teacher_name StudentID 
Foundations of Catholic Worldview Honors THE409 A+ Mr. M 1 
US Government Honors SOC405 A Mrs. H 1 
US Government Honors SOC405 B- Mr. H 1 
Social Justice THE404 A Mrs. C 2 
Psychology Honors SOC605 A- Mrs. E 2 
Forensics SCI334 A Mrs. R 2 
Social Justice THE404 A+ Mr. H 3 
US Government Honors SOC405 B Mrs. C 3 
Peace and Justice I Honors THE407 A+ Mrs. E 3 
Peace and Justice I THE406 A- Mrs. R 4 
Peace and Justice I THE406 A+ Mrs. E 4 
Economics Honors SOC415 A- Mrs. R 4 
Peace and Justice I THE406 A- Mr. H 4 
US Government SOC400 A+ Mr. L 5 
Peace and Justice I Honors THE407 A+ Mr. B 5 
Social Justice Honors THE405 A+ Mrs. C 5 

考試成績來從4個表格:

測試t (給我們表名)

ID Name 
3 PSAT 

TestScores TS (讓我們從測試需要什麼樣的小分)

ID Name TestID 
51 PSAT_Critical Reading 3 
53 PSAT_Math 3 

StudentTest ST (告訴我們的學生需要什麼樣的測試,該測試的日期,以及多少次學生取試驗)

ID StudentID TestID Test_Date 658 1 3 2015年12月1日998 2 3 2015年12月1日100 2 3 2016年12月1日 1354 3 3 2014年12月1日 1500 3 3 2015年12月1日 1688 3 3 2016年12月1日 8875 4 3 2015年12月1日 3364 5個3 2015年12月1日

StudentTestScore STS (告訴我們如何將學生的分數在測試。例如,大鳥得到了他的批判性閱讀PSAT測試78%)

ID NumScore StudentID StudentTestID TestScoreID 
573 78 1 658 51 
574 65 1 658 53 
385 62 2 998 51 
387 68 2 998 53 
889 89 2 100 51 
892 92 2 100 53 
112 58 3 1354 51 
113 59 3 1354 53 
6544 68 3 1688 51 
6546 62 3 1688 53 
8882 75 3 1500 51 
8889 79 3 1500 53 
425 98 4 8875 51 
426 97 4 8875 53 
658 82 5 3364 51 
659 86 5 3364 53 

這裏是我想什麼:

ID Student_Number lastfirst grade_level Teacher_name course_name Course_number Grade Test Date Test Name Best PSAT Reading Score Best PSAT Math Score 
1 1 Bird, Big 9 Mr. M Foundations of Catholic Worldview Honors THE409 A+ 12/1/2015 PSAT 78 65 
1 1 Bird, Big 9 Mrs. H US Government Honors SOC405 A 12/1/2015 PSAT 78 65 
1 1 Bird, Big 9 Mr. H US Government Honors SOC405 B- 12/1/2015 PSAT 78 65 
2 2 Bob, Sponge 10 Mrs. C Social Justice THE404 A 12/1/2016 PSAT 89 92 
2 2 Bob, Sponge 10 Mrs. E Psychology Honors SOC605 A- 12/1/2016 PSAT 89 92 
2 2 Bob, Sponge 10 Mrs. R Forensics SCI334 A 12/1/2016 PSAT 89 92 
3 3 Man, He 9 Mr. H Social Justice THE404 A+ 12/1/2015 PSAT 75 79 
3 3 Man, He 9 Mrs. C US Government Honors SOC405 B 12/1/2015 PSAT 75 79 
3 3 Man, He 9 Mrs. E Peace and Justice I Honors THE407 A+ 12/1/2015 PSAT 75 79 
4 4 Rah, She 10 Mrs. R Economics Honors SOC415 A- 12/1/2015 PSAT 98 97 
4 4 Rah, She 10 Mr. H Peace and Justice I THE406 A- 12/1/2015 PSAT 98 97 
5 5 Myers, Michael 11 Mr. L US Government SOC400 A+ 12/1/2015 PSAT 82 86 
5 5 Myers, Michael 11 Mr. B Peace and Justice I Honors THE407 A+ 12/1/2015 PSAT 82 86 
5 5 Myers, Michael 11 Mrs. C Social Justice Honors THE405 A+ 12/1/2015 PSAT 82 86 

什麼我得到的是:

ID Student_Number lastfirst grade_level Teacher_name course_name Course_number Grade Test Date Test Name Best PSAT Reading Score Best PSAT Math Score 
4 4 Rah, She 10 Mrs. R Economics Honors SOC415 A- 12/1/2015 PSAT 98 97 
4 4 Rah, She 10 Mr. H Peace and Justice I THE406 A- 12/1/2015 PSAT 98 97 

這是拉動學生證4號,因爲他們有最好的測試結果。我需要每個人的個人最佳測試結果。

我很感激你能給我的任何幫助!

回答

0

您還沒有提供任何樣本數據,因此以下是推論。

查詢返回了很多無關列,像老師的名字,當然,這意味着你多行每名學生。

您的查詢似乎包含參數化學生ID的連接,這可能是爲什麼您只有一個學生獲得行。

此查詢使用解析RANK()函數來確定每個學生和子測試的最高分數。

SELECT StuNumber 
     , StuName 
     , GradeLevel 
     , test_name 
     , MAX(CASE WHEN test_subtest ='PSAT_CriticalReading' THEN Num_Score ELSE null END) as ReadingTest 
     , MAX(CASE WHEN test_subtest ='PSAT_Math' THEN Num_Score ELSE null END) as MathematicsTest 
FROM ( 
    SELECT s.Student_Number as StuNumber 
      , s.lastfirst as StuName 
      , s.Grade_Level as GradeLevel 
      , t.name as Test_Name 
      , ts.Name as Test_Subtest 
      , sts.NumScore as Num_Score 
      , rank() over (partition by s.Student_Number, ts.Name order by sts.NumScore) as score_rank 
    FROM Students s 
    INNER JOIN StudentTest st ON s.ID = st.studentID 
    INNER JOIN StudentTestScore sts ON sts.StudentTestID = st.ID   
    INNER JOIN Test t ON st.TestID = t.ID 
    INNER JOIN TestScore ts ON ts.ID = sts.TestScoreID 
    WHERE t.name = 'PSAT' 
    AND s.Enroll_Status=0 
    AND s.Grade_Level >=9 
    AND s.schoolid = ~(curschoolid) 
    ) TST 
AND score_rank = 1 
GROUP BY StuNumber 
      , StuName 
      , GradeLevel 
      , test_name 

在rank = 1上過濾子查詢可防止包含其他分數。不包括教師姓名和其他東西應該給你每個學生一行。 「應該」,因爲在沒有樣本數據的情況下,我無法測試這個。刪除studentId上的過濾器意味着包括所有的學生。

如果你有一個的實際需求返回在你的問題中顯示的更廣泛的結果集,你將需要以不同的方式滿足它。


順便說一下,這​​3210不會做你認爲它的作用。將日期轉換爲字符串意味着排序使用ASCII值,因此12/31/2013高於01/01/2016。我們可以對Oracle日期進行算術運算:這給出了預期的結果:MAX(test_date)

+0

嗨APC,謝謝你看我的數據。我編輯並添加了更好的描述。我確實需要更廣泛的結果,所以我認爲這看起來完全不同。思考? – jlinds