1
我有一個這個表; Sql Server:學生計數和組標記
<table><tbody><tr><th>StudentID</th><th>AcademicYear</th><th>Term</th><th>StudentName</th><th>Class</th><th>Marks</th><th>Position</th><th>Count</th></tr><tr><td>12013</td><td>2015</td><td>One</td><td>Adamu A. Msangi</td><td>Form IV</td><td>80</td><td>1</td><td>5</td></tr><tr><td>52013</td><td>2015</td><td>One</td><td>Angela M. Lucas</td><td>Form IV</td><td>67</td><td>2</td><td>5</td></tr><tr><td>22014</td><td>2014</td><td>Two</td><td>Agness Daudi Martin</td><td>Form II</td><td>60</td><td>3</td><td>5</td></tr><tr><td>32014</td><td>2014</td><td>Two</td><td>Agness John Missana</td><td>Form II</td><td>51</td><td>4</td><td>5</td></tr><tr><td>42013</td><td>2015</td><td>One</td><td>Andrea F. Agustino</td><td>Form IV</td><td>50</td><td>5</td><td>5</td></tr></tbody></table>
這我通過運行該SQL語句:
SELECT
*,
ROW_NUMBER() OVER (ORDER BY Marks DESC) AS Position,
COUNT(StudentID) OVER() AS 'Count'
FROM
Student
我要找應顯示學生的位置和學生總數的輸出。例如,如果我查詢ID號的學生; 42013和期限=「一」和AcademicYear = 2015和class =「IV型」,我應該得到下表類似的結果:
<table><tbody><tr><th>StudentID</th><th>AcademicYear</th><th>Term</th><th>StudentName</th><th>Class</th><th>Marks</th><th>Position</th><th>Count</th></tr><tr><td>42013</td><td>2015</td><td>One</td><td>Andrea F. Agustino</td><td>Form IV</td><td>50</td><td>5</td><td>3</td></tr></tbody></table>
這是我嘗試沒有成功;
SELECT
*,
ROW_NUMBER() OVER (ORDER BY Marks DESC) AS Position,
COUNT(StudentID) OVER() AS 'Count'
FROM
Student
WHERE
StudentID = 42013
AND Term = 'One'
AND AcademicYear = 2015
AND Class = 'Form IV'
這怎麼辦?
感謝
感謝@GurV的快速回復,有沒有辦法讓每學期,班級的學生正確的號碼,AcademicYear的目標/相應的Student.For例如上面的查詢應顯示Student_count列中的3名學生。再次感謝 – Sandet
@Sandet我想你想分區。更新了我的答案。你能嘗試讓我知道它是否有幫助嗎? – GurV
感謝@GurV它正在工作... – Sandet