Solved

How can i get gap in dates in a table

Posted on 2011-02-10
4
283 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

821 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