2009-06-09 34 views
0

這是我正在嘗試解決的問題。我有一個表格,用於存儲StartExclusionDate和EndExclusionDate,併爲我的對象設置ApplyDate(稱爲「排除」)。表中可以有許多ExtId條目。SQL查詢查找單個排序記錄

我想從傳遞給查詢的日期包含在StartExclusionDate和EndExclusionDate之間的最後一個連續記錄中獲取ApplyDate。

因此,這裏是我在2005年SQL創建表:

Id int PK 
ExtId int FK 
StartExclusionDate datetime null 
EndExclusionDate datetime null 
ApplyDate not null 

這裏是一個示例數據集:

ID | ExtID | StartDate | EndDate | ApplyDate 
-------------------------------------------------------- 
    1  101  11/15/2005 3/15/2010 11/15/2005 
    2  101        12/30/2005 
    3  101  1/1/2000  1/1/2000  1/1/2006 
    4  101  4/1/2008     4/1/2008 
    5  101     8/1/2010  6/1/2008 
    6  101  11/1/2006 12/31/2010 11/1/2009 

我會傳遞2個參數,@ExtId和@dtDate。我想基於以下條件檢索記錄:@ExtId = 101, @dtDate = '6/15/2008'ID = 4

的邏輯是這樣的:

  • 獲取所有記錄EXTID = 101,並在那裏ApplyDate < =「6/15/2008' ,排序ApplyDate DESC
  • 如果第一個記錄的開始和結束日期之間是‘2008年6月15日’,發現其中排除最初通過檢查下一個記錄

我想創下的紀錄找到一種沒有C的方法URSOR,可以使用查詢或CTE嗎?

編輯: 這與光標

IF OBJECT_ID('TempDB..#t_edr','U') IS NOT NULL 
    DROP TABLE #t_edr 

CREATE TABLE #t_edr 
(
    Id INT NOT NULL, 
    ExtId INT NOT NULL, 
    StartDate DATETIME NULL, 
    EndDate DATETIME NULL, 
    ApplyDate DATETIME NOT NULL 
) 

DECLARE @ExtID INT, @dtDate DATETIME, @dtReturn DATETIME 
SELECT @ExtID = 101, @dtDate = '6/15/2008' 

SET NOCOUNT ON 
INSERT INTO #t_edr VALUES (1, 101, '11/15/2005', '3/15/2010', '11/15/2005') 
INSERT INTO #t_edr VALUES (2, 101, NULL, NULL, '12/30/2005') 
INSERT INTO #t_edr VALUES (3, 101, '1/1/2000', '1/1/2000', '1/1/2006') 
INSERT INTO #t_edr VALUES (4, 101, '4/1/2008', NULL, '4/1/2008') 
INSERT INTO #t_edr VALUES (5, 101, NULL, '8/1/2010', '6/1/2008') 
INSERT INTO #t_edr VALUES (6, 101, '11/1/2006', '12/31/2010', '6/1/2009') 
SET NOCOUNT OFF 

IF EXISTS (
    SELECT EDR.ID FROM #t_edr EDR 
    INNER JOIN (SELECT TOP 1 ID FROM #t_edr WHERE ExtId = @ExtID AND ApplyDate <= @dtDate ORDER BY ApplyDate DESC) LatestEDR 
    ON EDR.ID = LatestEDR.ID 
    WHERE ExtId = @ExtID 
    AND ApplyDate <= @dtDate  
    AND (StartDate IS NULL OR StartDate <= @dtDate) 
    AND (EndDate IS NULL OR EndDate >= @dtDate) 
    ) 
    BEGIN 
     DECLARE @ID INT, @StartEDR DATETIME, @EndEDR DATETIME, @ApplyDate DATETIME, 
      @CurrentApplyDate DATETIME, @Continue BIT 

     DECLARE c CURSOR LOCAL FAST_FORWARD FOR  
     SELECT Id, StartDate, EndDate, ApplyDate FROM #t_edr 
     WHERE ExtId = @ExtID AND ApplyDate <= @dtDate ORDER BY ApplyDate DESC 

     OPEN c 

     FETCH NEXT FROM c INTO @id, @startedr, @endedr, @ApplyDate 
     IF @@FETCH_STATUS <> 0 SET @Continue = 0 
     ELSE SET @Continue = 1 

     SET @CurrentApplyDate = @ApplyDate 
     WHILE @Continue = 1 
     BEGIN         
      IF @StartEDR >= @dtDate OR @EndEDR <= @dtDate 
       SET @Continue = 0 

      IF @Continue = 1 
      BEGIN 
       SET @CurrentApplyDate = @ApplyDate 
       FETCH NEXT FROM c INTO @id, @startedr, @endedr, @ApplyDate 
       IF @@FETCH_STATUS <> 0 SET @Continue = 0 
      END 
     END 
     CLOSE c 
     DEALLOCATE c 

     SELECT @CurrentApplyDate 
    END 
ELSE 
    PRINT 'NOT EXCLUDED' 

DROP TABLE #t_edr 

回答

2
select top 1 * from table 
where ApplyDate <= '4/15/2008' 
and (startdate is null or startdate<= '4/15/2008') 
and (enddate is null or enddate >= '4/15/2008') 
and extid = '101' 
order by ApplyDate desc 
+0

這將返回第一條記錄(ID = 1)完成的,因爲它匹配的起始日期和結束日期的標準 – 2009-06-09 15:34:33