Solved

SQL Query for detecting Date sequence break

Posted on 2008-06-25
3
1,093 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

688 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