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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:28:57+00:00 2026-05-27T09:28:57+00:00

I am trying to use SQL to select distinct data entries based on the

  • 0

I am trying to use SQL to select distinct data entries based on the time difference between one entry and the next. It’s easier to explain with an example:

My data table has

Part    DateTime   
123     12:00:00
123     12:00:05
123     12:00:06
456     12:10:23
789     12:12:13
123     12:14:32

I would like to return all rows as long with the limitation that if there are multiple entries with the same “Part” number I would like to retrieve only those that have a difference of at least 5 minutes.

The query should return:

Part    DateTime   
123     12:00:00
456     12:10:23
789     12:12:13
123     12:14:32

The code I’m using is the following:

SELECT data1.*, to_char(data1.scan_time, 'yyyymmdd hh24:mi:ss') 

FROM data data1 

where exists
(
    select * 

    from data data2

    where data1.part_serial_number = data2.part_serial_number AND 
    data2.scan_time + 5/1440 >= data1.scan_time 
    and data2.info is null
)

order by to_char(data1.scan_time, 'yyyymmdd hh24:mi:ss'), data1.part_serial_number

This is not working unfortunately. Does anyone know what i’m doing wrong or can suggest an alternate approach??

Thanks

  • 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-27T09:28:58+00:00Added an answer on May 27, 2026 at 9:28 am

    Analytic functions to the rescue.

    You can use the analytic function LEAD to get the data for the next row for the part.

    SQL> ed
    Wrote file afiedt.buf
    
      1  with x as (
      2    select 123 part, timestamp '2011-12-08 00:00:00' ts
      3      from dual
      4    union all
      5    select 123, timestamp '2011-12-08 00:00:05'
      6      from dual
      7    union all
      8    select 123, timestamp '2011-12-08 00:00:06'
      9      from dual
     10    union all
     11    select 456, timestamp '2011-12-08 00:10:23'
     12      from dual
     13    union all
     14    select 789, timestamp '2011-12-08 00:12:13'
     15      from dual
     16    union all
     17    select 123, timestamp '2011-12-08 00:14:32'
     18      from dual
     19  )
     20  select part,
     21         ts,
     22         lead(ts) over (partition by part order by ts) next_ts
     23*   from x
    SQL> /
    
          PART TS                              NEXT_TS
    ---------- ------------------------------- -------------------------------
           123 08-DEC-11 12.00.00.000000000 AM 08-DEC-11 12.00.05.000000000 AM
           123 08-DEC-11 12.00.05.000000000 AM 08-DEC-11 12.00.06.000000000 AM
           123 08-DEC-11 12.00.06.000000000 AM 08-DEC-11 12.14.32.000000000 AM
           123 08-DEC-11 12.14.32.000000000 AM
           456 08-DEC-11 12.10.23.000000000 AM
           789 08-DEC-11 12.12.13.000000000 AM
    
    6 rows selected.
    

    Once you’ve done that, then you can create an inline view and simply select those rows where the next date is more than 5 minutes after the current date.

    SQL> ed
    Wrote file afiedt.buf
    
      1  with x as (
      2    select 123 part, timestamp '2011-12-08 00:00:00' ts
      3      from dual
      4    union all
      5    select 123, timestamp '2011-12-08 00:00:05'
      6      from dual
      7    union all
      8    select 123, timestamp '2011-12-08 00:00:06'
      9      from dual
     10    union all
     11    select 456, timestamp '2011-12-08 00:10:23'
     12      from dual
     13    union all
     14    select 789, timestamp '2011-12-08 00:12:13'
     15      from dual
     16    union all
     17    select 123, timestamp '2011-12-08 00:14:32'
     18      from dual
     19  )
     20  select part,
     21         ts
     22    from (
     23      select part,
     24             ts,
     25             lead(ts) over (partition by part order by ts) next_ts
     26        from x )
     27   where next_ts is null
     28*     or next_ts > ts + interval '5' minute
    SQL> /
    
          PART TS
    ---------- -------------------------------
           123 08-DEC-11 12.00.06.000000000 AM
           123 08-DEC-11 12.14.32.000000000 AM
           456 08-DEC-11 12.10.23.000000000 AM
           789 08-DEC-11 12.12.13.000000000 AM
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the T-SQL function DATEDIFF to select the number of distinct
I'm trying to use LINQ to SQL to select a few specific columns from
Trying to use the following 'ANY' syntax and gets an error: SELECT DISTINCT Em.ename
Basically I'm trying to do this in LINQ to SQL; SELECT DISTINCT a,b,c FROM
I'm trying to use SQL like select column from table where column in (?)
I am trying to use a SQL Select statement for a query in Java.
I'm trying to use SQL Server 2000 DTS Package to download a file from
I'm trying to use SQL Server Profiler (2005) to track down some application performance
I am trying to use a SQL Server 2008 Ranking Function on a query
Trying to use Linq to SQL for a small project I'm working on at

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.