If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Find date sequence gaps

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-05, 14:56
radiohead radiohead is offline
Registered User
 
Join Date: Jul 2005
Posts: 5
Find date sequence gaps

I have a table with the field:

datesold

The dates are not in chronological order. Using only a query (without perl etc) I would like to traverse the dates returning a list of beginning and ending dates that border a date gap greater than some value of days.

version 4.1

Any help much appreciated.
Reply With Quote
  #2 (permalink)  
Old 12-05-05, 05:20
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by radiohead
I would like to traverse the dates returning a list of beginning and ending dates that border a date gap greater than some value of days.
Code:
SELECT
  f1.datesold AS d1,
  (SELECT MIN(f2.datesold) FROM foo f2 WHERE f2.datesold > f1.datesold) AS d2
FROM foo f1
HAVING d1 < d2 - INTERVAL 5 DAY
ORDER BY d1;
--
felix
Reply With Quote
  #3 (permalink)  
Old 12-06-05, 12:01
radiohead radiohead is offline
Registered User
 
Join Date: Jul 2005
Posts: 5
The query works very well, thanks. I added a distinct to prevent redundant checking (actually I dont know if it prevents redundant checking, but at least removes them from display):

SELECT
distinct(f1.datesold) AS d1,
(SELECT MIN(f2.datesold) FROM foo f2 WHERE f2.datesold > f1.datesold) AS d2
FROM foo f1
HAVING d1 < d2 - INTERVAL 5 DAY
ORDER BY d1;

Is there anyway to optimize the query to run quicker? It takes minutes to run it on a table with 100,000 entries, maybe 2500 unique dates.

Thanks again.
Reply With Quote
  #4 (permalink)  
Old 12-06-05, 12:34
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by radiohead
Is there anyway to optimize the query to run quicker? It takes minutes to run it on a table with 100,000 entries, maybe 2500 unique dates.
Do you have an index on datesold?

--
felix
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On