Home » SQL & PL/SQL » SQL & PL/SQL » retrieving previous status code (oracle sql)
retrieving previous status code [message #628147] Thu, 20 November 2014 07:40 Go to next message
silver_fox
Messages: 1
Registered: November 2014
Location: Canada
Junior Member
I am trying to retrieve the previous status code that is different from the current. The last 2 records may have the same status code but the previous one is different.

tried using decode with a lag, it shows in the prev_case_status_code column but i want it to be the only record displayed and the example below shows it grabbing the previous status_code, i need it to grab the previous status _code when there is a possibility of there being the same current status_code in the last 2 records. I am probably not using the right code.......help!!

SELECT t.case_id,t.end_date,t.case_status_code, decode(t.case_id,lag(t.case_id,1,0) over (order by t.case_id,
t.end_date), lag(t.case_status_code,1,0) over (order by t.case_id, t.end_date),0) as prev_case_status_code
from DMICM.TV_CASE_STATUS t
where t.case_id = 1042113
ORDER BY t.end_date

case_id end_date case_status_code previous status code
1 1042113 14/05/2008 1649 0
2 1042113 06/06/2008 1558 1649
3 1042113 06/06/2008 1216 1558
4 1042113 31/10/2008 1649 1216
5 1042113 16/01/2009 1558 1649
6 1042113 16/01/2009 1216 1558
7 1042113 31/10/2009 1649 1216
8 1042113 26/11/2009 1558 1649
9 1042113 26/11/2009 1216 1558
10 1042113 06/07/2010 1649 1216
11 1042113 04/08/2010 1505 1649
12 1042113 06/06/2011 1558 1505
13 1042113 06/06/2011 1216 1558
14 1042113 30/11/2011 1649 1216
15 1042113 05/12/2011 1558 1649
16 1042113 05/12/2011 1216 1558
17 1042113 31/12/4712 1649 1216

[Updated on: Thu, 20 November 2014 09:09]

Report message to a moderator

Re: retrieving previous status code [message #628155 is a reply to message #628147] Thu, 20 November 2014 09:10 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: retrieving previous status code [message #628156 is a reply to message #628147] Thu, 20 November 2014 09:20 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Previous Topic: Pattern Marching using regexp
Next Topic: column names are row values from another table.
Goto Forum:
  


Current Time: Fri Apr 19 17:33:53 CDT 2024