Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to write a query to add an updated column in based on other columns?

M PNov 14 2018 — edited Nov 19 2018

Hey everyone,

I have a database that has columns like the below table and I want to write a query to have a new column to correct the information in the type column. The logic is if there's a C type record after an L type record and the END time is larger than the L type record and they have the same ID, the type should be updated to L. One important thing that makes this query complicated for me is if there's a second L type record for the same ID, I should compare the next C record to the second L type, nit the first one. For example, sequence 5 should be compared to sequence 4 not the sequence 1.

ID SequenceType
End time
1234A1L5/13/2018  4:00:00 PM
1234A2C5/13/2018  3:00:00 PM
1234A3C5/13/2018  4:30:00 PM
1234A4L5/13/2018  5:00:00 PM
1234A5C5/13/2018  5:45:00 PM

In summary, the result of the query should be like this:

D SequenceType
End timeTrue Type
1234A1I5/13/2018  4:00:00 PMI
1234A2C5/13/2018  3:00:00 PMC
1234A3C5/13/2018  4:30:00 PMI
1234A4I5/13/2018  5:00:00 PMI
1234A5C5/13/2018  5:45:00 PMI

Thank you in advance, and I look forward to your guidelines.

This post has been answered by mathguy on Nov 14 2018
Jump to Answer
Comments
Post Details
Added on Nov 14 2018
16 comments
542 views