So, I've got some history tables that have a begin date and an end date. The problem is, that there's several records in this table that refer to the same thing, but their begin dates and end dates are not exact. So, I'm trying to unify their begin and end dates.
So, each set of record will have close begin and end dates (within about 7 seconds). Then there will be another cluster, with the same key (in this case, VoyageID), but a different set of close begin and end dates. Does that make sense? I can post some sample data if it doesn't.
Anyway, my goal right now is to find the minimum begin date for each cluster. What I have now gets me the minimum for each VoyageID. Any help would be appreciated. Thanks!
Here's what I have:
DECLARE @7S DATETIME
SET @7S = '0:0:07'
PRINT @7S
SELECT MAX(T1.BeginDate), T1.VoyageID FROM
hist.VoyageProfitLossValues T1 INNER JOIN
hist.VoyageProfitLossValues T2 ON
T1.VoyageID = T2.VoyageID AND
T1.BeginDate BETWEEN (T2.BeginDate - @7S) and (T2.BeginDate + @7S)
GROUP BY T1.VoyageID
EDIT: Sample data:
BeginDate EndDate VoyageID
2011-07-05 07:02:50.713 2011-07-05 07:25:53.007 6312
2011-07-05 07:02:50.870 2011-07-05 07:25:53.693 6312
2011-07-05 07:02:51.027 2011-07-05 07:25:54.387 6312
2011-07-08 14:22:21.147 NULL 6312
2011-07-08 14:22:21.163 NULL 6312
2011-07-08 14:22:21.177 NULL 6312
Note: The real data has more than 3 per each voyage, and the BeginDates can be further apart.
And I would want out of this:
BeginDate VoyageID
2011-07-05 07:02:50.713 6312
2011-07-08 14:22:21.147 6312
What I have will just give me the first line.
I'll eventually do this with the end date, as well, but I can convert one to the other easily.
GROUP BYdates within a 7-second range?