Solved

How can i get gap in dates in a table

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now