I've seen some brilliant answers on here and I am in need of a fix. I do not want to create table or ETL just yet and I was hoping to create a simple database view users could access to test first.
A table has an item#, item_ticket_color#, maint_date, and other columns tick_col is updated sometimes but other columns in the table are as well. If some other column is updated or changed tick_col iterates. I need to filter through this data to get the below expected outcome and need some help trying to figure out how I might do this. I am looking for straight SQL if possible
ITEM......................TICK_COL................. MAINT_DATE
21524804....................RIBG...................1/1/0001
21524804....................RIBG...................6/15/2008 6:52:57 AM
21524804....................RIBG...................6/25/2008 11:31:03 AM
21524804....................RIBG...................6/28/2008 4:12:21 AM
21524804....................RIWH...................9/20/2008 6:36:24 AM
21524804....................RIGR...................9/23/2008 6:36:44 AM
21524804....................RIGR...................9/30/2008 6:37:42 AM
21524804....................RIWH...................10/31/2008 6:37:27 AM
21524804....................RIWH...................11/1/2008 6:36:41 AM
21524804....................RIGR...................3/11/2009 6:01:43 PM
21524804....................RIGR...................7/28/2009 6:37:11 AM
21524804....................RIGR...................10/8/2009 6:37:00 AM
21524804....................RIBS...................11/20/2009 6:37:58 AM
21524804....................RIBS...................5/18/2010 6:37:07 AM
21524804....................RIBS...................9/16/2010 6:38:11 AM
21524804....................RIBS...................8/13/2012 10:39:44 AM
21524804....................RIBS...................3/12/2013 6:46:08 AM
21524804....................RIBS...................3/17/2013 9:25:31 AM
21524804....................RIBS...................3/27/2013 6:52:57 AM
21524804....................RIBS...................7/25/2013 6:41:51 AM
I am Expecting to see this below, where it shows the start and end date for each of the scenarios where the ticket color changed.:
21524804.....RIBG.....10101........20080919
21524804.....RIWH.....20080920.....20080922
21524804.....RIGR.....20080923.....20081030
21524804.....RIWH.....20081031.....20090310
21524804.....RIGR.....20090311.....20091119
21524804.....RIBS.....20091120.....20130725
here is the new code, based on Jasti's contribution, I made some additional changes but this is exactly what I wanted
SELECT item,
tick_col,
from_dt,
CASE
WHEN LEAD (from_dt) OVER (PARTITION BY item ORDER BY from_dt) - 1
IS NULL
THEN
SYSDATE
ELSE
LEAD (from_dt) OVER (PARTITION BY item ORDER BY from_dt) - 1
END
TO_DATE
FROM ( SELECT ITEM,
TICK_COL,
MIN (MAINT_DATE) AS from_dt,
MAX (MAINT_DATE) AS to_dt
FROM (SELECT SUM (start_of_group) OVER (ORDER BY maint_date) AS sm,
ITEM,
TICK_COL,
maint_date
FROM (SELECT ITEM,
TICK_COL,
maint_date,
CASE
WHEN LAG (TICK_COL, 1, TICK_COL)
OVER (ORDER BY maint_date) =
TICK_COL
THEN
0
ELSE
1
END
start_of_group
FROM mytable))
GROUP BY ITEM, TICK_COL, sm
ORDER BY sm)