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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:23:24+00:00 2026-06-11T16:23:24+00:00

I am working with a table that, for each ID, has a list of

  • 0

I am working with a table that, for each ID, has a list of “events”, where an event is whether or not the object represented by that ID went “On” or “Off.” Items assumingly start as off if they are not in this table. An item can only legally go on if was not on at that point.

Here’s an example of what it might look like:

Events
ID   Type    Date
1    On      01-MAY-12
1    Off     01-JUN-12
1    On      05-JUN-12

Unfortunately, this data is terrible and it is filled with impossibilities. Here is what part of this table actually looks like:

Events
ID   Type    Date
1    On      01-MAY-12
1    On      01-MAY-12
1    On      01-JUN-12
1    Off     01-JUL-12
1    Off     01-AUG-12
1    On      05-AUG-12

What I want to do is write a query that returns this table without junk data, where impossible rows (that is, rows that turn the item on when it’s already on, or off when it’s already off) are removed.

I don’t care too much about the case of it being ‘off’ before being turned on for the first time as I don’t believe we have any cases of that.

So, said query would return:

Events
ID   Type    Date
1    On      01-MAY-12
1    Off     01-JUL-12
1    On      01-AUG-12

Any thoughts on how to do this? This logic of “return the first row under condition X” is causing me a lot of difficulty. If I just wanted the first on row or first off row, period, I could just group by and grab a min. But how do I do it in this case?

  • 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-11T16:23:25+00:00Added an answer on June 11, 2026 at 4:23 pm

    You can write this:

    SELECT id,
           type,
           "date"
      FROM ( SELECT id,
                    LAG(type) OVER (PARTITION BY id ORDER BY "date") AS prev_type,
                    type,
                    "date"
               FROM events
           )
     WHERE type <> NVL(prev_type, '-')
     ORDER
        BY "date"
    ;
    

    Notes:

    • date isn’t really a valid column name unless you wrap it in double-quotes. I’m guessing that’s not really the name of the column?
    • You wrote, “I don’t care too much about the case of it being ‘off’ before being turned on for the first time as I don’t believe we have any cases of that”, so I decided not to remove initial Offs, but that’s actually no harder: just change NVL(prev_type, '-') to NVL(prev_type, 'Off').
    • I didn’t worry about the case that that you have two records with the same id and "date" and different types, because I had no idea how you wanted that handled. I hope you don’t need them to be put in whatever-order-makes-them-possible, because that would be significantly harder.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a legacy database table that has a phone no. field but
The database I'm working against has table names such as table_name. That's fine, but
Working on a website that has Employee and Branch entities, using a database table
I'm working on an table drill-down style iPhone app that has prepopulated data. I
I working on a table that keeps record of student attending course. The data
A site I'm working on takes table data, counts that table data and uses
I'm working on a project that will have a single table holding lots and
I am working on a website that auto populates search result in a table
Hi I have been working on this code that creates a table with radio
I am working on a form that adds employee information to a MySQL table

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.