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 7736015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:38:41+00:00 2026-06-01T07:38:41+00:00

The problem is that it takes way to long in SQL and there must

  • 0

The problem is that it takes way to long in SQL and there must be a better way. I’ve picked out the slow part for the scenario bellow.
Scenario:
Two (temp) tables with event times for start and end for vehicles that have to be paired up to figure idle durations. The issue is that some of the event data is missing. I figured out a rudimentary way of going through and determining when the last end time is after the next start time and removing the invalid start. Again not elegant + very slow.

Tables :

create table #start(VehicleIp int null,                                         CurrentDate datetime null,
                    EventId int null, 
                    StartId int null)
create table #end(VehicleIp int null, 
        CurrentDate datetime null,
        EventId int null, 
        EndId int null)

–//Note: StartId and EndId are both pre-filled with something like:

ROW_NUMBER() Over(Partition by VehicleIp order by VehicleIp,  CurrentDate)

–//Slow SQL

while exists(
            select top 1 tOn.EventId 
            from #start as tOn 
            left JOIN #end tOff 
            on tOn.VehicleIp = tOff.VehicleIp and 
            tOn.StartID = tOff.EndID +1
     )
begin

      declare @badEntry int

      select top 1 @badEntry = tOn.EventId  
      from #s as tOn 
            left JOIN #se tOff  
              on tOn.VehicleIp = tOff.VehicleIp and 
                 tOn.StartID = tOff.EndID +1 
       order by tOn.CurrentDate 

       delete from #s where EventId  = @badEntry 

      ;with _s as ( select VehicleIp, CurrentDate, EventId, 
                           ROW_NUMBER() Over(Partition by VehicleIp 
                           order by VehicleIp, CurrentDate) StartID 
                     from #start)
      update #start 
  set StartId = _s.StartId 
  from #s join _s on #s.EventId  = _s.EventId 

end 
  • 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-06-01T07:38:42+00:00Added an answer on June 1, 2026 at 7:38 am

    Assuming you start with a table containing Vehicle and interval in which it was used, this query will identify gaps.

    select b.VehicleID, b.IdleStart, b.IdleEnd
    from
       (
          select VehicleID, 
           -- If EndDate is not inclusive, remove +1
                 EndDate + 1 IdleStart, 
           -- First date after current for this vehicle
           -- If you don't want to show unused vehicles to current date remove isnull part 
                 isnull((select top 1 StartDate 
                    from TableA a 
                   where a.VehicleID = b.VehicleID
                     and a.StartDate > b.StartDate
                   order by StartDate
                 ), getdate()) IdleEnd
            from TableA b
       ) b
    where b.IdleStart < b.IdleEnd
    

    If dates have time portion they should be truncated to required precision, here is for day:

    dateadd(dd, datediff(dd,0, getDate()), 0)
    

    Replace dd with hh, mm or whatever precision is needed.

    And here is Sql Fiddle with test

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everything works fine, the only problem is that it loads very slow. It takes
I have a small problem that takes me for a day and until now
I have a problem here that requires to design a data structure that takes
I'm having a problem with this query that takes several seconds to complete. I
I'm having a problem with a cocoa application that takes the value of a
Simple problem that I can't figure out... How can I print a '%' character
I remember reading a while back that randomly SQL Server can slow down and
The problem is that if I execute a select that takes minutes, all other
in a bash script I am implementing some functions, that take parameters The problem
Link: http://www.fraynepainting.com/services The problem is that div#dditem_2 (the div with the Take a look...

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.