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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:01:58+00:00 2026-06-12T00:01:58+00:00

I have a problem with a query. This is the data (order by Timestamp):

  • 0

I have a problem with a query.

This is the data (order by Timestamp):

Data
    ID  Value   Timestamp
    1   0       2001-1-1
    2   0       2002-1-1
    3   1       2003-1-1
    4   1       2004-1-1
    5   0       2005-1-1
    6   2       2006-1-1
    7   2       2007-1-1
    8   2       2008-1-1

I need to extract distinct values and the first occurance of the date. The exception here is that I need to group them only if not interrupted with a new value in that timeframe.
So the data I need is:

ID  Value   Timestamp
1   0       2001-1-1
3   1       2003-1-1
5   0       2005-1-1
6   2       2006-1-1

I’ve made this work by a complicated query, but am sure there is an easier way to do it, just cant think of it. Could anyone help?

This is what I started with – probably could work with that. This is a query that should locate when a value is changed.

  > SELECT * FROM Data d1 join Data d2 ON d1.Timestamp < d2.Timestamp and
    > d1.Value <> d2.Value

It probably could be done with a good use of row_number clause but cant manage it.

  • 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-12T00:02:03+00:00Added an answer on June 12, 2026 at 12:02 am

    Sample data:

    declare @T table (ID int, Value int, Timestamp date)
    insert into @T(ID,  Value,   Timestamp) values
    (1,   0,       '20010101'),
    (2,   0,       '20020101'),
    (3,   1,       '20030101'),
    (4,   1,       '20040101'),
    (5,   0,       '20050101'),
    (6,   2,       '20060101'),
    (7,   2,       '20070101'),
    (8,   2,       '20080101')
    

    Query:

    ;With OrderedValues as (
        select *,ROW_NUMBER() OVER (ORDER By TimeStamp) as rn --TODO - specific columns better than *
        from @T
    ), Firsts as (
        select
            ov1.* --TODO - specific columns better than *
        from
            OrderedValues ov1
                left join
            OrderedValues ov2
                on
                    ov1.Value = ov2.Value and
                    ov1.rn = ov2.rn + 1
        where
            ov2.ID is null
    )
    select * --TODO - specific columns better than *
    from Firsts
    

    I didn’t rely on the ID values being sequential and without gaps. If that’s the situation, you can omit OrderedValues (using the table and ID in place of OrderedValues and rn). The second query simply finds rows where there isn’t an immediate preceding row with the same Value.

    Result:

    ID          Value       Timestamp  rn
    ----------- ----------- ---------- --------------------
    1           0           2001-01-01 1
    3           1           2003-01-01 3
    5           0           2005-01-01 5
    6           2           2006-01-01 6
    

    You can order by rn if you need the results in this specific order.

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

Sidebar

Related Questions

I have a problem to order a query as a tree as this WITH
I have a problem with this query trying to get a sum and a
I have a problem with a query. This query works with phpMyAdmin, but I
I have problem with my query on C, I’m using the oci8 driver. This
I have a problem. I want to use a LINQ query against this database:
I have a strange problem. I do this query in a node.js server using
I have a nHibernate query like this ICriteria query = session.CreateCriteria(typeof(MyResult)) .Add(Expression.Eq(ResultTypeId, myResult.ResultTypeId)) Problem
I'm having a misunderstanding problem with this use of Linq Query I do have
I have a problem in my select statement in my query. This sql query
This is my MySQL query and I have a week problem in this query.

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.