Skip to content
Advertisement

How to get all rows from a SQL table before a change in column string?

I have these 2 tables and their inserts:

I have this query to partition them by JobId and order them by JobId and StatusDtTm:

enter image description here

I want to fetch only the rows where the Job last finished per job. Basically get all the records per job before it last finished (highlighted in green). I can’t seem to figure out what to add in the where clause here to get the highlighted result. Can someone help please?

Advertisement

Answer

One approach is to add the following column to your query:

This new field sel will have a nonzero value for the highlighted rows, where a current or a previous StatusName for that job ID has the value ‘Finished’; otherwise, the value will be zero.

The full query to output only the highlighted records is then:

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