在MySQL中查找结束/开始日期

时间:2009-11-27 00:55:26

标签: mysql

看看这个MySQL表:

+------------+-----------+
| date_key   | stude_key |
+------------+-----------+
| 2005-09-01 | COM       | 
| 2005-09-02 | COM       | 
| 2005-09-06 | COM       | 
| 2005-09-07 | COM       | 
| 2005-09-08 | COM       | 
| 2005-09-09 | COM       | 
| 2005-09-12 | COM       | 
| 2005-09-01 | YUM       | 
| 2005-09-02 | YUM       | 
| 2005-09-06 | YUM       | 
| 2005-09-07 | YUM       | 
| 2005-09-08 | YUM       | 
| 2005-09-09 | YUM       | 
| 2005-09-12 | YUM       | 
| 2005-09-01 | DSA       | 
| 2005-09-02 | DSA       | 
| 2005-09-06 | DSA       | 
| 2005-09-07 | DSA       | 
| 2005-09-01 | JRA       | 
+------------+-----------+

它包含课程开始和学生出勤的日子。我想知道放弃的学生(停止上课)和什么时候。学生不能错过课程并返回。

例如,该课程于2005年9月1日至12日举行.COM和YUM参加了所有课程,但DSA放弃了2009-09-07而JRA只获得了1个课程。

我正试图找到一张桌子:

+------------+-----------+
| gave_up    | student   |
+------------+-----------+
| 2005-09-07 | DSA       | 
| 2005-09-01 | JRA       | 
+------------+-----------+

关于如何有效地做到这一点的任何想法?

由于

2 个答案:

答案 0 :(得分:4)

SELECT MAX(date_key) AS gave_up, stude_key AS student
 FROM thetable
 GROUP BY stude_key
 HAVING MAX(date_key) != '2005-09-12'

答案 1 :(得分:1)

如果你刚刚在每个学生的最后一个条目之后:

SELECT MAX(date_key) AS gave_up, student
FROM [table_name]
GROUP BY student