Solved

SQL Query for detecting Date sequence break

Posted on 2008-06-25
3
1,059 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sqlquerystress - To test db performance 8 39
Mongo DB 18 37
Oracle Insert not working 10 22
Insert query into temp tables using Coldfusion 3 13
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

813 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

17 Experts available now in Live!

Get 1:1 Help Now