日期中的SQL差距

时间:2012-08-06 16:06:42

标签: sql tsql

我试图根据表格的状态代码在表格中找到空白。

StateTable:

StateID (PK) | Code
--------------------
1            | AK
2            | AL
3            | AR

StateModel表:

StateModelID | StateID | EfftiveDate            | ExpirationDate
-------------------------------------------------------------------------
1            |  1      | 2012-06-28 00:00:00.000| 2012-08-02 23:59:59.000
2            |  1      | 2012-08-03 00:00:00.000| 2050-12-31 23:59:59.000
3            |  1      | 2055-01-01 00:00:00.000| 2075-12-31 23:59:59.000

我正在使用的查询如下:

Declare @gapMessage varchar(250)
SET @gapMessage = ''

select
@gapMessage = @gapMessage + 
  (Select StateTable.Code FROM StateTable where t1.StateID = StateTable.StateID) 
  + ' Row ' +CAST(t1.StateModelID as varchar(6))+' has a gap with '+
  CAST(t2.StateModelID as varchar(6))+ CHAR(10)
   from StateModel t1
   inner join StateModel t2
    on
       t1.StateID = t2.StateID
       and DATEADD(ss, 1,t1.ExpirationDate) < t2.EffectiveDate
       and t1.EffectiveDate < t2.EffectiveDate

 if(@gapMessage != '')
 begin         
Print 'States with a gap problem'
PRINT @gapMessage
 end
 else
 begin
PRINT 'No States with a gap problem'
 end

但是通过上面的表示例,我得到以下输出:

States with a gap problem
AK Row 1 has a gap with 3
AK Row 2 has a gap with 3

是否有重构我的查询,以便1和3之间的差距不显示,因为1和2之间没有差距? 我正在使用MS sql server 2008 感谢

3 个答案:

答案 0 :(得分:3)

WITH
  sequenced AS
(
  SELECT
    ROW_NUMBER() OVER (PARTITION BY StateID ORDER BY EfftiveDate) AS SequenceID,
    *
  FROM
    StateModel
)
SELECT
  *
FROM
  sequenced    AS a
INNER JOIN
  sequenced    AS b
    ON  a.StateID    = b.StateID
    AND a.SequenceID = b.SequenceID - 1
WHERE
  a.ExpirationDate < DATEADD(second, -1, b.EfftiveDate)

为了使其尽可能有效,还要在(StateID, EfftiveDate)

上添加索引

答案 1 :(得分:1)

我想对MatBailie表示赞赏,但是还没有意见,所以我想我会帮助其他人寻找类似的解决方案,可能需要更进一步,就像我需要的那样至。我已将我的代码(涉及成员注册)的应用程序更改为与此处示例相同的语言。

就我而言,我需要这些东西:

  1. 我有两个类似的表需要开发成一个总表。在这个例子中,让我们像这样制作表:SomeStates + OtherStates = UpdatedTable。这些是AS子句中的UNIONED。
  2. 我不想因为间隙而删除任何行,但我想在StateID级别上标记它们。这是作为附加列“StateID_GapFlag”添加的。
  3. 我还想添加一列来保存最旧的或MIN(EffectiveDate)。这将用于稍后的SUM(周期)计算,以获得总持续时间,不包括间隙。这是“MIN_EffectiveDate”列。

    ;WITH sequenced     
    ( SequenceID
    ,EffectiveDate
    ,ExpirationDate)
    AS
        (select 
        ROW_NUMBER() OVER (PARTITION BY StateID ORDER by EffectiveDate) as SequenceID,
        * from  (select EffectiveDate, ExpirationDate from SomeStates
                    UNION ALL
                (select EffectiveDate, ExpirationDate from OtherStates)
                ) StateModel
        where 
            EffectiveDate > 'filter'
        )
    Select DISTINCT 
            IJ1.[MIN_EffectiveDate]
            ,coalesce(IJ2.GapFlag,'') as [MemberEnrollmentGapFlag]
            ,EffectiveDate
            ,ExpirationDate
    into UpdatedTable 
    from sequenced seq
            inner join 
                (select StateID, min(EffectiveDate) as 'MIN_EffectiveDate'
                    from sequenced 
                    group by StateID
                ) IJ1
            on seq.member# = IJ1.member
            left join
                (select a.member#, 'GAP' as 'StateID_GapFlag'
                    from sequenced a
                    inner join 
                        sequenced b
                            on a.StateID = b.StateID
                            and a.SequenceID = (b.sequenceID - 1)
                        where a.ExpirationDate < DATEADD(day, -1, b.EffectiveDate)
                ) LJ2
            on seq.StateID = LJ2.StateID
    

答案 2 :(得分:0)

您可以使用ROW_NUMBER为每个州提供stateModel的排序,然后检查连续行的第二个差异是否超过1.类似于:

;WITH Models (StateModelID, StateID, Effective, Expiration, RowOrder) AS (
    SELECT StateModelID, StateID, EffectiveDate, ExpirationDate,
           ROW_NUMBER() OVER (PARTITION BY StateID, ORDER BY EffectiveDate)
    FROM StateModel
)
SELECT F.StateModelId, S.StateModelId
FROM Models F
CROSS APPLY (
    SELECT M.StateModelId
    FROM Models M
    WHERE M.RowOrder = F.RowOrder + 1
      AND M.StateId = F.StateId
      AND DATEDIFF(SECOND, F.Expiration, M.Effective) > 1
) S

这将为您提供具有间隙的行的状态模型ID,您可以按照自己的意愿格式化。