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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:12:58+00:00 2026-05-29T08:12:58+00:00

I am a long time fan of Stack Overflow but I’ve come across a

  • 0

I am a long time fan of Stack Overflow but I’ve come across a problem that I haven’t found addressed yet and need some expert help.
I have a query that is sorted chronologically with a date-time compound key (unique, never deleted) and several pieces of data. What I want to know is if there is a way to find the start (or end) of a region where a value changes? I.E.

DateTime someVal1 someVal2 someVal3 target

  1. 1 3 4 A
  2. 1 2 4 A
  3. 1 3 4 A
  4. 1 2 4 B
  5. 1 2 5 B
  6. 1 2 5 A

and my query returns rows 1, 4 and 6. It finds the change in col 5 from A to B and then from B back to A? I have tried the find duplicates method and using min and max in the totals property however it gives me the first and last overall instead of the local max and min? Any similar problems?

  • 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-29T08:13:00+00:00Added an answer on May 29, 2026 at 8:13 am

    I didn’t see any purpose for the someVal1, someVal2, and someVal3 fields, so I left them out. I used an autonumber as the primary key instead of your date/time field; but this approach should also work with your date/time primary key. This is the data in my version of your table.

    pkey_field target
    1          A
    2          A
    3          A
    4          B
    5          B
    6          A
    

    I used a correlated subquery to find the previous pkey_field value for each row.

    SELECT
        m.pkey_field,
        m.target,
        (SELECT Max(pkey_field)
            FROM YourTable
            WHERE pkey_field < m.pkey_field)
        AS prev_pkey_field
    FROM YourTable AS m;
    

    Then put that in a subquery which I joined to another copy of the base table.

    SELECT
        sub.pkey_field,
        sub.target,
        sub.prev_pkey_field,
        prev.target AS prev_target
    FROM
        (SELECT
            m.pkey_field,
            m.target,
            (SELECT Max(pkey_field)
                FROM YourTable
                WHERE pkey_field < m.pkey_field)
            AS prev_pkey_field
        FROM YourTable AS m) AS sub
        LEFT JOIN YourTable AS prev
        ON sub.prev_pkey_field = prev.pkey_field
    WHERE
        sub.prev_pkey_field Is Null
        OR prev.target <> sub.target;
    

    This is the output from that final query.

    pkey_field target prev_pkey_field prev_target
    1          A  
    4          B      3               A
    6          A      5               B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been a big fan of this site for a long time, but
I'll preface this by saying that I've been a longtime fan of Stack Overflow,
I'm a long running fan of stackoverflow, first time poster. I'd love to see
long time reader, first time poster. I’m coming with an issue that many of
Not long time before I've discovered, that new dynamic keyword doesn't work well with
Long time back in past I had read somewhere that if we want to
Long time lurker, first time poster. I have a page that can dynamically (with
Long time lurker, first time poster. I have found some good answers on here
A long time back, I read a great article that describes a cross-browser friendly
For long time, I was always thinking that parameters in Perl subs are passed

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.