Solved

How can i get gap in dates in a table

Posted on 2011-02-10
4
287 Views
Last Modified: 2012-06-22
How can i get gap in dates in a table in SQl.I have one table which contains address for 1000 diferent memebrs with start date and end date ..one mmeebr can have mutiple address in this table .

I want to find gapes in address for all members.
0
Comment
Question by:KChoudhary
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34864241
Please post the structure for you table (CREATE TABLE ...) so that we get a better idea of what you are trying to achieve.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 34865446
Hi,

I'm not going to claim that the following code is the best ever, or that the join is particularly good, only that I believe that it is one solution to your problem.

I'm assuming here that if the address is current there is no endDate - ie, EndDate is null.

Negative values on the DaysGap is an overlap, which may or maynot be part of your data.

The key is to join the current row with the next row - next being a concept that is a little alien to SQL which treats everything as unordered sets, so the join condition is trying to order that set.

HTH
  David


use tempdb
go

if object_id('dbo.AddressHistory', 'u') is not null
  drop table dbo.AddressHistory
go

if object_id('dbo.AddressHistory', 'u') is null
	create table dbo.AddressHistory(
		AdressHistoryID int identity not null
		, AddressID int not null
		, AddressLine1 nvarchar( 60 )
		, StartDate datetime
		, EndDate datetime
		, constraint PK_AddressHistory primary key clustered ( AdressHistoryID )
	)
;
go

truncate table dbo.AddressHistory
go

insert dbo.AddressHistory( 
	AddressID
	--, AddressLine1
	, StartDate
	, EndDate
	)
	values
		( 1, '2008-08-03', null )
		, ( 1, '2005-03-23', '2008-01-06' )
		, ( 1, '2000-01-01', '2001-01-01' )
		, ( 1, '2008-01-06', '2008-07-13' )
		, ( 1, '2000-12-15', '2002-03-25' )
;

select *
from dbo.AddressHistory
;

select *
from dbo.AddressHistory ah
order by
	ah.StartDate
;

select 
	ah.AddressID
	, ah.StartDate
	, ah.EndDate
	, ahn.StartDate
	, ahn.EndDate
	, datediff( day, ah.EndDate, ahn.StartDate ) as DaysGap
from dbo.AddressHistory ah
left outer join dbo.AddressHistory ahn
	on ahn.AddressID = ah.AddressID
	and ahn.StartDate = (
		select min( ahi.StartDate )
		from dbo.AddressHistory ahi
		where
			ahi.StartDate > ah.StartDate
                            ahi.AddressID = ah.AddressID
		)
where
	ah.EndDate is not null
order by
	ah.StartDate
;

-- Results
AddressID   StartDate               EndDate                 StartDate               EndDate                 DaysGap
----------- ----------------------- ----------------------- ----------------------- ----------------------- -----------
1           2000-01-01 00:00:00.000 2001-01-01 00:00:00.000 2000-12-15 00:00:00.000 2002-03-25 00:00:00.000 -17
1           2000-12-15 00:00:00.000 2002-03-25 00:00:00.000 2005-03-23 00:00:00.000 2008-01-06 00:00:00.000 1094
1           2005-03-23 00:00:00.000 2008-01-06 00:00:00.000 2008-01-06 00:00:00.000 2008-07-13 00:00:00.000 0
1           2008-01-06 00:00:00.000 2008-07-13 00:00:00.000 2008-08-03 00:00:00.000 NULL                    21

(4 row(s) affected)

Open in new window

0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 34865475
Whoops

line 62 should start with and
select 
	ah.AddressID
	, ah.StartDate
	, ah.EndDate
	, ahn.StartDate
	, ahn.EndDate
	, datediff( day, ah.EndDate, ahn.StartDate ) as DaysGap
from dbo.AddressHistory ah
left outer join dbo.AddressHistory ahn
	on ahn.AddressID = ah.AddressID
	and ahn.StartDate = (
		select min( ahi.StartDate )
		from dbo.AddressHistory ahi
		where
			ahi.StartDate > ah.StartDate
			and ahi.AddressID = ah.AddressID
		)
where
	ah.EndDate is not null
order by
	ah.StartDate
;

Open in new window

0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 34865893
Can you explain more clearly what is this "Gap in dates" and what column name are you using to lookup dates? Please give more detailed instructions on what you are trying to achieve. Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question