列出所有每天最多写一次考试的学生

时间:2015-07-07 03:53:47

标签: sql-server-2008

我有3张表,如学生,主题和中期表。

学生表包含

studid Firstname lastname Class

1      A       R        12A
2      B       S        12A
3      C       T        12A
4      D       U        12A
5      E       V        12B

SUBJECT表包含

subid subname
  1     maths
  2     science
  3     english

MIDTERM表包含

studid subid marks examdate
1        1    100  2014-09-24
1        2     92  2014-09-25
1        2     92  2014-09-26
2        1     74  2014-09-24
2        2     78  2014-09-26
2        3     73  2014-09-26
3        1     90  2014-09-24
3        2     84  2014-09-25
3        2     92  2014-09-25
5        1     87  2014-09-24
4        2     79  2014-09-24
4        3     90  2014-09-26

1 个答案:

答案 0 :(得分:0)

select * from Student

EXCEPT
--list of students with more than one subject per day

select A.* from Student A 
join
(select studid, examdate, count(distinct subid)cnt group by studid, examdate from [Midterm]
)B
on A.Studid = B.Studid
and B.cnt >1