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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:16:02+00:00 2026-06-05T08:16:02+00:00

I have the following table which contains values read every 15 minutes from several

  • 0

I have the following table which contains values read every 15 minutes from several different devices:

ID   DeviceID   Date                    Value
----------------------------------------------
1    3          24.08.2011 00:00:00     0.51
2    3          24.08.2011 00:15:00     2.9
3    3          24.08.2011 00:30:00     0
4    3          24.08.2011 00:45:00     7.1
5    3          24.08.2011 01:00:00     1.05
6    3          24.08.2011 03:15:00     3.8

I’d like to find all the gaps in the table for each device, where there are no entries, for a given month. For the table above, the result should be something like this:

DeviceID    StartDate               EndDate
-------------------------------------------------------
3           24.08.2011 01:00:00     24.08.2011 03:15:00

The table has roughly 35000 devices and 100 million entries.

This is what I tried; it’s rather slow, but returns what I need. However, besides its speed, there’s another problem: it only finds missing intervals up to the last entry for a device in the given month; anything after that will be ignored, so it’s possible to miss an extra interval of missing values.

SELECT
    t2.Date AS StartDate
    , t1.Date AS EndDate
FROM
    TestTable t1
    INNER JOIN TestTable t2 ON t1.DeviceID = t2.DeviceID
WHERE
    (t2.Date = (SELECT MAX(Date) FROM TestTable t3 WHERE t3.Date < t1.Date AND t3.DeviceID = t1.DeviceID)
        AND DATEDIFF(MINUTE, t2.Date, t1.Date) > 15)
    AND t1.DeviceID = @id
    AND DATEPART(YEAR, t1.Date) = @year AND DATEPART(MONTH, t1.Date) = @month
  • 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-05T08:16:03+00:00Added an answer on June 5, 2026 at 8:16 am

    Following should work and doesn’t return just a single record for a deviceid.

    The gist of this is to

    • Add a rownumber to each record, ordered by Date and restarting for each DeviceID.
    • Join with self to create a result with rows consisting of the combination of two original rows. The relation between the columns of each row is the rownumber (+1) and the DeviceID.
    • Only retain those rows where the related Date is more than 15 minutes.

    SQL Statement

    ;WITH t AS (
      SELECT  *, rn = ROW_NUMBER() OVER (PARTITION BY DeviceID ORDER BY Date)
      FROM    TestTable
    )  
    SELECT  t1.DeviceID, t1.Date, t2.Date
    FROM    t t1
            INNER JOIN t t2 ON t2.DeviceID = t1.DeviceID AND t2.rn = t1.rn + 1
    WHERE   DATEDIFF(MINUTE, t1.Date, t2.Date) > 15        
    

    Test script

    ;WITH TestTable (ID, DeviceID, Date, Value) AS (
      SELECT 1, 3, '2011-08-24 00:00:00', 0.51 UNION ALL
      SELECT 2, 3, '2011-08-24 00:15:00', 2.9 UNION ALL
      SELECT 3, 3, '2011-08-24 00:30:00', 0 UNION ALL
      SELECT 4, 3, '2011-08-24 00:45:00', 7.1 UNION ALL
      SELECT 5, 3, '2011-08-24 01:00:00', 1.05 UNION ALL
      SELECT 6, 3, '2011-08-24 03:15:00', 3.8 
    )
    , t AS (
      SELECT  *, rn = ROW_NUMBER() OVER (PARTITION BY DeviceID ORDER BY Date)
      FROM    TestTable
    )  
    SELECT  t1.DeviceID, t1.Date, t2.Date
    FROM    t t1
            INNER JOIN t t2 ON t2.DeviceID = t1.DeviceID AND t2.rn = t1.rn + 1
    WHERE   DATEDIFF(MINUTE, t1.Date, t2.Date) > 15        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column c in table tbl which contains the following values. Tue,
i have a table Students which contains the following colums: Id,FirstName,LastName,Adress . The colum
I have a table of surveys which contains (amongst others) the following columns survey_id
I have a table which contains the following columns and I am trying to
I have a varchar column which contains date in following format dd/mm/yyyy I need
I have a table A with column code which contains values AB,BC ----- code
i have a table in which a row contains following data. So i need
I have the following table structure inside a view which gets displayed in the
I have the following situation: An Conversations entity/table which has multiple Tags associated to
I have the following table structure, which is imported into an Entity Framework project:

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.