我想選擇所有那些條目如下的條目:下一行的開始日期應該小於END的1個月END前一行的日期ie(Startdate(row i)< Enddate (行i-1))爲相同的'GpID'和'客戶id'。從不同的行和不同的列中選擇數據
例如,在下面的屏幕截圖,行與[ClientGPID] = 2
是無效的,因爲與[ClientGPID] = 2
行的Startdate
是行的> END date
與[ClientGPID] = 1
爲GPID = 67
CREATE TABLE [Client_GP]
(
[ClientGPID] [int] IDENTITY(1,1) NOT NULL,
[ClientID] [int] NOT NULL,
[GpID] [int] NOT NULL,
[Startdate] [datetime] NOT NULL,
[Enddate] [datetime] NULL
)
GO
INSERT INTO [Client_GP]
VALUES (67, 812, '2016-01-28 16:49:28.000', '2016-01-28 17:52:01.000');
INSERT INTO [Client_GP]
VALUES(67, 812, '2017-02-24 16:49:28.000', '2017-02-24 17:52:01.000');
INSERT INTO [Client_GP]
VALUES(67, 812, '2017-02-24 17:52:06.000', NULL);
你能顯示你想要的輸出嗎? –