按最小和最大日期分组

时间:2012-07-09 14:32:13

标签: sql ms-access gaps-and-islands

date        |  name      | go to work 
------------+------------+---------
02/01/2012  | Mike       |  train
03/01/2012  | Mike       |  train
04/01/2012  | Mike       |  bus
07/01/2012  | Mike       |  bus
08/01/2012  | Mike       |  bus
09/01/2012  | Mike       |  train

有了这些记录,我想创建一个表:

name     from        to              go to work 
------------------------------------------------
Mike     02/01/2012  03/01/2012        train
Mike     04/01/2012  08/01/2012        bus
Mike     09/01/2012  09/01/2012        train

我不知道如何在Access中编写SQL代码。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

SELECT  name, MIN(`date`) AS from, MAX(`date`) AS to 
FROM your_table
GROUP BY name, go_to_work