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

  • Home
  • SEARCH
  • 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 839939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:32:38+00:00 2026-05-15T05:32:38+00:00

We have a number of machines which record data into a database at sporadic

  • 0

We have a number of machines which record data into a database at sporadic intervals. For each record, I’d like to obtain the time period between this recording and the previous recording.

I can do this using ROW_NUMBER as follows:

WITH TempTable AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Machine_ID ORDER BY Date_Time) AS Ordering
    FROM dbo.DataTable
)

SELECT [Current].*, Previous.Date_Time AS PreviousDateTime
FROM TempTable AS [Current]
INNER JOIN TempTable AS Previous 
    ON [Current].Machine_ID = Previous.Machine_ID
    AND Previous.Ordering = [Current].Ordering + 1

The problem is, it goes really slow (several minutes on a table with about 10k entries) – I tried creating separate indicies on Machine_ID and Date_Time, and a single joined-index, but nothing helps.

Is there anyway to rewrite this query to go faster?

  • 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-15T05:32:38+00:00Added an answer on May 15, 2026 at 5:32 am

    How does it compare to this version?:

    SELECT x.*
        ,(SELECT MAX(Date_Time)
          FROM dbo.DataTable
          WHERE Machine_ID = x.Machine_ID
              AND Date_Time < x.Date_Time
        ) AS PreviousDateTime
    FROM dbo.DataTable AS x
    

    Or this version?:

    SELECT x.*
        ,triang_join.PreviousDateTime
    FROM dbo.DataTable AS x
    INNER JOIN (
        SELECT l.Machine_ID, l.Date_Time, MAX(r.Date_Time) AS PreviousDateTime
        FROM dbo.DataTable AS l
        LEFT JOIN dbo.DataTable AS r
        ON l.Machine_ID = r.Machine_ID
            AND l.Date_Time > r.Date_Time
        GROUP BY l.Machine_ID, l.Date_Time
    ) AS triang_join
    ON triang_join.Machine_ID = x.Machine_ID
        AND triang_join.Date_Time = x.Date_Time
    

    Both would perform best with an index on Machine_ID, Date_Time and for correct results, I’m assuming that this is unique.

    You haven’t mentioned what is hidden away in * and that can sometimes means a lot since a Machine_ID, Date_Time index will not generally be covering and if you have a lot of columns there or they have a lot of data, …

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

Sidebar

Related Questions

I have a number of polygons which covers some rectangle. Each polygons is associated
I have a number of machines, all of which have Visual Studio installed, where
On a number of machines (with Asp.net 1 and 2) we have seen the
I have a number of .txt files. I would like to concatenate those and
I'm building an application which will have dynamic allocated objects of type A each
For an application which is deployed on a large number of machines I took
I have a need to run a relatively large number of virtual machines on
I have an application which get copied and run on client machines. The program
Hi have created a listbox which is bound to list of machines. As the
I have inherited a Java project which includes a number of classes that are

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.