Solved

How can i get gap in dates in a table

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Acces SQL Insert Problem 6 28
SQL Log size 3 16
SQL Find Carriage Return and Delete it. 3 11
MS SQL + group by time 4 12
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 setup several different housekeeping processes for a SQL Server.

790 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