Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:21:28+00:00 2026-05-26T18:21:28+00:00

So, I’ve got some history tables that have a begin date and an end

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:21:29+00:00Added an answer on May 26, 2026 at 6:21 pm

    The idea of this solution is to order your rows on BeginDate for each VoyageID. Go from the top and pick the rows that have a time diff of more than 7 seconds to the previous row.

    @Voy is instead of hist.VoyageProfitLossValues. First I create a temp table #T that will fill the ID column with ordered values for each VoyageID. C is a recursive CTE that starts at ID = 1 and runs through all rows comparing current row with the previous row and storing the result in column FirstDate. I added a second VoyageID to the sample data just to prove it works with that as well.

    declare @Voy table
    (
      BeginDate datetime,
      EndDate datetime,
      VoyageID int
    )
    
    insert into @Voy values  
    ('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),
    ('2011-07-05 07:02:50.713',     '2011-07-05 07:25:53.007',    6313),
    ('2011-07-05 07:02:50.870',     '2011-07-05 07:25:53.693',    6313),
    ('2011-07-05 07:02:51.027',     '2011-07-05 07:25:54.387',    6313),
    ('2011-07-08 14:22:21.147',      NULL                    ,    6313),
    ('2011-07-08 14:22:21.163',      NULL                    ,    6313),
    ('2011-07-08 14:22:21.177',      NULL                    ,    6313)
    
    
    create table #T
    (
      ID int,
      VoyageID int,
      BeginDate datetime
      primary key (ID, VoyageID)
    )
    
    insert into #T (ID, VoyageID, BeginDate)
    select row_number() over(partition by VoyageID order by BeginDate),
           VoyageID,
           BeginDate
    from @Voy     
    
    
    ;with C as
    (
      select T.ID,
             T.VoyageID,
             T.BeginDate,
             1 as FirstDate
      from #T as T
      where T.ID = 1
      union all
      select T.ID,
             T.VoyageID,
             T.BeginDate,
             case when datediff(second, C.BeginDate, T.BeginDate) > 7 then 1 else 0 end
      from #T as T
        inner join C
          on T.ID = C.ID + 1 and
             T.VoyageID = C.VoyageID
    )
    select C.BeginDate,
           C.VoyageID
    from C
    where C.FirstDate = 1
    order by C.VoyageID,
             C.BeginDate
    option (maxrecursion 0)
    
    
    drop table #T
    

    Result:

    BeginDate               VoyageID
    ----------------------- -----------
    2011-07-05 07:02:50.713 6312
    2011-07-08 14:22:21.147 6312
    2011-07-05 07:02:50.713 6313
    2011-07-08 14:22:21.147 6313
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.