Solved

SQL Query for detecting Date sequence break

Posted on 2008-06-25
3
1,046 Views
Last Modified: 2013-12-07
I have one table in SQL Server 2000 database with the following structure.

ID (Identity Column)
User ID
Amount
Measure Date (DateTime column)

The users enters one record per day per user in this table. Sometimes if user is not operational then Admin can override and enter the 0 amount data for user. Admin can also enter this data in advance for future dates. So the date column data can look like the following for a particuler user.

1/1/2008
1/2/2008
1/3/2008
1/4/2008
1/10/2008
1/11/2008
1/12/2008
1/13/2008

Now you can notice that there are no dates between 1/5 and 1/9.

My question is how can I identify the break in date sequence using sql query. In above example I want to get either 1/4 or 1/5 as sql query result.

Thanks,

0
Comment
Question by:borenl
3 Comments
 
LVL 3

Expert Comment

by:GHCS_Mark
ID: 21870861
You could have a function called PreviousDate() that returns
SELECT TOP 1 CurrentDate FROM Table1 WHERE CurrentDate < @PassedDate ORDER BY CurrentDate DESC

And then use the following select statement:

SELECT CurrentDate, dbo.PreviousDate(CurrentDate) as PreviousDate, DATEDIFF(D,CurrentDate,dbo.PreviousDate(CurrentDate)) AS DaysDifference
0
 
LVL 15

Accepted Solution

by:
ishando earned 500 total points
ID: 21872111
How about

select MeasureDate
from table1 t1
where exists (select 1 from table1 t2 where t2.UserId = t1.UserId and t2.MeasureDate > t1.MeasureDate)
and not exists (select 1 from table1 t3 where t3.UserId = t1.UserId and t3.MeasureDate = t1.MeasureDate+1)
0
 

Author Closing Comment

by:borenl
ID: 31470799
Thanks lot.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

947 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

21 Experts available now in Live!

Get 1:1 Help Now