Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:53:01+00:00 2026-06-09T04:53:01+00:00

I am trying to find gaps in the a table based on a state

  • 0

I am trying to find gaps in the a table based on a state code the tables look like this.

StateTable:

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

StateModel Table:

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

The query I am using is the following:

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

But with the above table example I get the following output:

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

Is there anyway to restructure my query so that the gap between 1 and 3 does not display because there is not a gap between 1 and 2?
I am using MS sql server 2008
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:53:02+00:00Added an answer on June 9, 2026 at 4:53 am
    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)
    

    To make this as effective as possible, also add an index on (StateID, EfftiveDate)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find all HTML <table> rows with this operator, but nothing: preg_match_all(#<tr[^>]*>.*</tr>#, $content,
Im trying to find a set of results in a database based on dates.
Trying to find some information on this but am unable to get any results
Am trying to find items based on item name.If items are present status bar
I'm trying find a way to create mysql users with passwords using php code,
Trying to find code to basically stream audio and visual streams over a network
trying to find an example of this, it's possible that I am not going
Trying to find the best way to write this SQL statement. I have a
Trying to find out what the (float) in this function means: static float avgEndpoints
Trying to find an XML file I can use in lieu of a look-up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.