Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can i get gap in dates in a table

Posted on 2011-02-10
4
Medium Priority
?
290 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
  • 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

927 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