也许尝试一个DISTINCT
与 a 结合的列表WHERE End > Start
,如这个简化的例子:
CREATE TABLE Shifts(ShiftCode CHAR, ShiftStart DATETIME, ShiftEnd DATETIME);
GO
INSERT Shifts
VALUES('A','2/26/2012 07:00:00','2/26/2012 15:00:00')
, ('B','2/26/2012 15:00:00','2/26/2012 23:00:00')
, ('C','2/26/2012 23:00:00','2/27/2012 07:00:00')
, ('A','2/27/2012 07:00:00','2/27/2012 15:00:00')
, ('B','2/27/2012 15:00:00','2/27/2012 23:00:00')
, ('C','2/27/2012 23:00:00','2/28/2012 07:00:00')
, ('A','2/28/2012 07:00:00','2/28/2012 15:00:00')
, ('B','2/28/2012 15:00:00','2/28/2012 23:00:00')
, ('C','2/28/2012 23:00:00','2/29/2012 07:00:00')
, ('A','2/29/2012 07:00:00','2/29/2012 15:00:00')
, ('B','2/29/2012 15:00:00','2/29/2012 23:00:00')
, ('C','2/29/2012 23:00:00','3/1/2012 07:00:00')
, ('A','3/1/2012 07:00:00','3/1/2012 15:00:00')
, ('B','3/1/2012 15:00:00','3/1/2012 23:00:00')
, ('C','3/1/2012 23:00:00','3/2/2012 07:00:00');
GO
SELECT DISTINCT ShiftCode
, ShiftStart = DATEADD(YYYY,1,ShiftStart)
, ShiftEnd = DATEADD(YYYY,1,ShiftEnd)
FROM Shifts
WHERE DATEADD(YYYY,1,ShiftEnd) > DATEADD(YYYY,1,ShiftStart)
ORDER BY DATEADD(YYYY,1,ShiftStart), ShiftCode
GO
结果:
A 2013-02-26 07:00:00.000 2013-02-26 15:00:00.000
B 2013-02-26 15:00:00.000 2013-02-26 23:00:00.000
C 2013-02-26 23:00:00.000 2013-02-27 07:00:00.000
A 2013-02-27 07:00:00.000 2013-02-27 15:00:00.000
B 2013-02-27 15:00:00.000 2013-02-27 23:00:00.000
C 2013-02-27 23:00:00.000 2013-02-28 07:00:00.000
A 2013-02-28 07:00:00.000 2013-02-28 15:00:00.000
B 2013-02-28 15:00:00.000 2013-02-28 23:00:00.000
C 2013-02-28 23:00:00.000 2013-03-01 07:00:00.000
A 2013-03-01 07:00:00.000 2013-03-01 15:00:00.000
B 2013-03-01 15:00:00.000 2013-03-01 23:00:00.000
C 2013-03-01 23:00:00.000 2013-03-02 07:00:00.000