Skip to content
Advertisement

SQL Informix advanced query

Let me try to explain what I want to do with my data. Structure of my data is as follows:

I have 3 columns: date, id, and stage. Stages can be different numbers, but I’m interested in showing specific transition of some id from stage -1 to stage 1. Please see the example below.

  1. I have an id that was in stage -1, then transited to stage 1, and went back to stage -1 later. Whenever the transition from -1 to 1 is made, I want that id.

  2. Here, I have transition from -1 to 3, and that is not what interest me.

  3. Here, transition is made, and this id is what I want to be shown.

  4. Here, we have transition from 1 to -1, and that is not what I want.

Data:

My desired output is:

I hope that I explained this clear enough.

Also, after this, i want to show transition from either -1 and 1 to stage 3.

Advertisement

Answer

You could use lag():

This brings all ids that have at least one transition from -1 to 1.

Also, after this, i want to show transition from either -1 and 1 to stage 3.

You can easily adapt the query to fit that use case as well. Just change the final where clause to:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement