Results 1 to 3 of 3

Thread: Query to find gap in begin date/end date series

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Query to find gap in begin date/end date series

    I want to find records that exist that demonstrate a certain problem we are having with some of our data. But only if it's easy to find these records. In other words, if this is at all complicated and not worth your time trying to explain it to me, that's fine. (That is because there is another developer working on another related issue which when he fixes, could possibly make this problem go away also).

    So I have records with an effective date and an end date. They should be consecutive. If I select a group of these records and order them by effective date, they should go 5/1/2010 to 4/30/2011, then 5/1/2011 to 4/30/2012, then 5/1/2012 to 4/30/2013 and finally 5/1/2013 to 12/31/9999. What I want to find are the ones that go 5/1/2010 to 9/30/2010 then 5/1/2011 to 4/30/12. So I want to look at the current record and examine the next and if the next record's effective date is not equal to the current record's end date plus one day, I would pull that record as a good test case. The effective date can be anything; I chose chose 5/1 as an example.

    Again, this is just a one-shot query to find data, so it doesn't have to be perfect and shouldn't take too long to develop or explain. What happened was users found examples by working with the data in the front-end, and then a manual update query was run to fix the data so it is in sequence, when what we want to do is prevent the out of sequence scenario in the first place. But I need to find bad data to understand how it got out of sequence to know where to direct the fix.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Query to find gap in begin date/end date series

    here's a query I've used before to get a list of days between a range...
    Code:
    declare @STARTDATE datetime
    declare @ENDDATE datetime
    
    set @STARTDATE = '2013-01-01'
    set @ENDDATE = '2013-01-31'
    ;with DATELIST as (
    	select @STARTDATE as DATEITEM
    	union all
    	select DATEADD(day, 1, DATEITEM) as DATEITEM from DATELIST
    	where DATEITEM < @ENDDATE 
    )
    select * from DATELIST
    As is, it goes by days... but could also be set to go by months or years even...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: Query to find gap in begin date/end date series

    I also found this (I actually googled to see if I could find my own post from what keywords I was entering and I ended up getting a lot of hit, which I should've done before I posted. I didn't realize this is a common programming problem).
    Code:
    CREATE TABLE Dates (
     dt DATETIME NOT NULL PRIMARY KEY);
     
    INSERT INTO Dates VALUES('20081204'); 
    INSERT INTO Dates VALUES('20081205');
    INSERT INTO Dates VALUES('20090608');
    INSERT INTO Dates VALUES('20090609');
    
    -- missing ranges
    SELECT DATEADD(DAY, 1, prev) AS start_gap,
           DATEADD(DAY, -1, next) AS end_gap,
           DATEDIFF(MONTH, DATEADD(DAY, 1, prev), 
                           DATEADD(DAY, -1, next)) AS month_diff
    FROM (
    SELECT dt AS prev, 
          (SELECT MIN(dt) 
           FROM Dates AS B
           WHERE B.dt > A.dt) AS next
    FROM Dates AS A) AS T
    WHERE DATEDIFF(DAY, prev, next) > 1
    which gives you:
    start_gap---------------------end_gap-------------------month_diff
    2008-12-06 00:00:00.000------2009-06-07 00:00:00.000--6
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width