2015-10-27 35 views
2

最低的8個值我有以下SQL表「S3results」:如何在得分表返回SQL

+-----------+----------+------------------+-------+ 
| Studentno | Fullname |  Subject  | Fmagg | 
+-----------+----------+------------------+-------+ 
| 100509 | Terry | Accounts   |  1 | 
| 100509 | Terry | Art    |  6 | 
| 100509 | Terry | Biology   |  3 | 
| 100509 | Terry | Chemistry  |  2 | 
| 100509 | Terry | Commerce   |  2 | 
| 100509 | Terry | Computer Studies |  4 | 
| 100509 | Terry | English   |  6 | 
| 100509 | Terry | Geography  |  1 | 
| 100509 | Terry | History   |  1 | 
| 100509 | Terry | Mathematics  |  3 | 
| 100509 | Terry | Physics   |  1 | 
| 100510 | Sena  | Accounts   |  4 | 
| 100510 | Sena  | Art    |  1 | 
| 100510 | Sena  | Biology   |  5 | 
| 100510 | Sena  | Chemistry  |  1 | 
| 100510 | Sena  | Commerce   |  3 | 
| 100510 | Sena  | Computer Studies |  3 | 
| 100510 | Sena  | English   |  4 | 
| 100510 | Sena  | Geography  |  1 | 
| 100510 | Sena  | History   |  4 | 
| 100510 | Sena  | Mathematics  |  1 | 
| 100510 | Sena  | Physics   |  2 | 
| 100511 | Cristen | Accounts   |  2 | 
| 100511 | Cristen | Art    |  1 | 
| 100511 | Cristen | Biology   |  2 | 
| 100511 | Cristen | Chemistry  |  1 | 
| 100511 | Cristen | Commerce   |  5 | 
| 100511 | Cristen | Computer Studies |  3 | 
| 100511 | Cristen | English   |  6 | 
| 100511 | Cristen | Geography  |  1 | 
| 100511 | Cristen | History   |  1 | 
| 100511 | Cristen | Mathematics  |  2 | 
| 100511 | Cristen | Physics   |  6 | 
+-----------+----------+------------------+-------+ 

我想是選擇8個學科得分最低的爲每一位學生在不管他們的得分如何,結果中應包括數學和英語的fmagg專欄。

下面

是我想要的結果:

+-----------+----------+------------------+-------+ 
| Studentno | Fullname |  Subject  | Fmagg | 
+-----------+----------+------------------+-------+ 
| 100509 | Terry | Accounts   |  1 | 
| 100509 | Terry | Geography  |  1 | 
| 100509 | Terry | History   |  1 | 
| 100509 | Terry | Physics   |  1 | 
| 100509 | Terry | Chemistry  |  2 | 
| 100509 | Terry | Commerce   |  2 | 
| 100509 | Terry | Mathematics  |  3 | 
| 100509 | Terry | English   |  6 | 
| 100510 | Sena  | Art    |  1 | 
| 100510 | Sena  | Chemistry  |  1 | 
| 100510 | Sena  | Geography  |  1 | 
| 100510 | Sena  | Mathematics  |  1 | 
| 100510 | Sena  | Physics   |  2 | 
| 100510 | Sena  | Commerce   |  3 | 
| 100510 | Sena  | Computer Studies |  3 | 
| 100510 | Sena  | English   |  4 | 
| 100511 | Cristen | Art    |  1 | 
| 100511 | Cristen | Chemistry  |  1 | 
| 100511 | Cristen | Geography  |  1 | 
| 100511 | Cristen | History   |  1 | 
| 100511 | Cristen | Accounts   |  2 | 
| 100511 | Cristen | Biology   |  2 | 
| 100511 | Cristen | Mathematics  |  2 | 
| 100511 | Cristen | English   |  6 | 
+-----------+----------+------------------+-------+ 

獲得前8名用下面的代碼幾乎爲我工作,但它不是我想要的

select StudentNo,FullName ,FMagg,Subject from S3Results where FMagg in (select distinct top 8 FMagg from S3Results where Subject ='Mathematics' or (select distinct top 8 FMagg from S3Results where Subject ='Mathematics' or Subject ='English' order by FMagg desc) 

任何幫助將高度讚賞

回答

0

您可以使用ROW_NUMBER窗口函數與common-table-expresssion(cte)結合使用。這工作 - 也許它並不完美,但至少它提供所需的輸出:

;WITH cte AS 
(SELECT Studentno, Fullname, [Subject], Fmagg, 
     CASE WHEN [Subject] IN ('Mathematics','English') 
      THEN 0 
      ELSE ROW_NUMBER() OVER (PARTITION BY Studentno ORDER BY Fmagg ASC) 
     END AS Ranking 
FROM S3Results) 

SELECT Studentno, Fullname, [Subject], Fmagg 
FROM cte 
WHERE Ranking < 8 
ORDER BY Studentno,Fmagg 

上面的代碼將始終提供8行(或更少,如果你沒有在你的基地臺8線)爲每個學生。所以你必須決定: 如果你想要每個學生總是需要8條線,那麼你可以用ROW_NUMBER。如果你想要6個最弱的分數加數學和英語,你應該看看用DENSE_RANK代替ROW_NUMBER。那麼你可能每個學生有9或更多的行。看看這一點,你就會明白一起的區別:

https://msdn.microsoft.com/de-at/library/ms189798.aspx

+0

感謝,這正是我一直在尋找 –

+0

對不起,我已經走了一會兒,上面的代碼爲我工作,直到我意識到,除非是英文,否則不包括等於或大於5的值。例如給予一個學生得分如下:5,2,3,5,2,3,3,4,7,7,1運行該查詢將產生:3,1,2,2,3,3,4。因爲你可以看到那些是7行而不是8我想要的。提前致謝。 –