这是我在 SQL-Sever 2005 模式下的数据
ID FromDate ToDate Diff
ZIM145876-01 03/01/2011 02/29/2012 1
ZIM145876-01 03/01/2012 02/28/2013 1
ZIM145876-01 03/01/2013 02/28/2014 NULL
ZIM145881-02 02/01/2012 03/31/2012 1
ZIM145881-02 04/01/2012 06/30/2012 1
ZIM145881-02 07/01/2012 09/30/2012 1
ZIM145881-02 10/01/2012 03/31/2013 1
ZIM145881-02 04/01/2013 06/30/2013 NULL
ZIM145878-01 05/15/2010 05/14/2011 201
ZIM145878-01 12/01/2011 11/30/2012 1
ZIM145878-01 12/01/2012 11/30/2013 NULL
现在在第一个案例中,我想要
ZIM145876-01 03/01/2011 02/28/2014
ZIM145881-02 02/01/2012 06/30/2013
但是在第三种情况下,我们有两个相同 ID 的占用日期,这就是我想要的
ZIM145878-01 05/15/2010 05/14/2011
ZIM145878-01 12/01/2011 11/30/2013
所以任何提示都会受到高度赞赏(SQLFiddle)