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)
任何幫助將高度讚賞
感謝,這正是我一直在尋找 –
對不起,我已經走了一會兒,上面的代碼爲我工作,直到我意識到,除非是英文,否則不包括等於或大於5的值。例如給予一個學生得分如下:5,2,3,5,2,3,3,4,7,7,1運行該查詢將產生:3,1,2,2,3,3,4。因爲你可以看到那些是7行而不是8我想要的。提前致謝。 –