用两个集合查找相邻行

时间:2014-05-13 18:24:24

标签: sql oracle

我有以下数据样本表。我需要通过tskid重复Eventtypes来弄清楚如何分组。 *我不能使用存储过程或循环。基本上对于每个tskid中的每个eventtype组,我希望使用第一个实例startdate作为startdate,并将每个重复组的最后一个实例startdate用作结束日期。请看下面我想要的输出。谢谢!

DATA:
with test as (
SELECT 'tbd123' AS TSKID,'20100928' AS STARTDATE,'14174646' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100928' AS STARTDATE,'16071919' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100929' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'10542121' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'11021516' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'13323839' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'16265253' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'16580202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20100930' AS STARTDATE,'17014949' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101001' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101001' AS STARTDATE,'10052525' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101001' AS STARTDATE,'10423434' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101004' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101004' AS STARTDATE,'17540506' AS TIMEOFDAY,'30' AS Eventtype
FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101020' AS STARTDATE,'10003132' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101020' AS STARTDATE,'12105253' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101021' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101022' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101025' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101026' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101027' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101028' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101029' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101101' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101101' AS STARTDATE,'12280606' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101101' AS STARTDATE,'12284444' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101102' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101103' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101104' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101105' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101105' AS STARTDATE,'10223636' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101105' AS STARTDATE,'13371212' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101108' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101109' AS STARTDATE,'300000' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101110' AS STARTDATE,'300202' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101111' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101112' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101115' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101116' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101117' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101118' AS STARTDATE,'300202' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101119' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101122' AS STARTDATE,'300303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101122' AS STARTDATE,'10134748' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101122' AS STARTDATE,'11554646' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101123' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101123' AS STARTDATE,'17024243' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101124' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101126' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101129' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101130' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101201' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101202' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101203' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101206' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101207' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101208' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101209' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101210' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101213' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101214' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101215' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101216' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101217' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101220' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101221' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101222' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101223' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101227' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101228' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101229' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101230' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20101231' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110103' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110104' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110105' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110106' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110107' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110110' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110111' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110112' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110113' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110114' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110118' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110119' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110120' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110121' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110124' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110125' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110126' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110127' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110128' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110131' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110201' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110202' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110203' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110204' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110608' AS STARTDATE,'15531212' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110620' AS STARTDATE,'17202222' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110623' AS STARTDATE,'9293273' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110623' AS STARTDATE,'10415757' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110623' AS STARTDATE,'10420183' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110623' AS STARTDATE,'14460845' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110624' AS STARTDATE,'9213939' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110624' AS STARTDATE,'9214444' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110708' AS STARTDATE,'8040505' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110801' AS STARTDATE,'12153434' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110801' AS STARTDATE,'14171313' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110808' AS STARTDATE,'11464092' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110808' AS STARTDATE,'16123640' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110809' AS STARTDATE,'9195607' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110809' AS STARTDATE,'9201911' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110809' AS STARTDATE,'11542121' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110912' AS STARTDATE,'9041752' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110912' AS STARTDATE,'9055948' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110912' AS STARTDATE,'16391168' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110912' AS STARTDATE,'16393185' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110913' AS STARTDATE,'14025206' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110915' AS STARTDATE,'9182121' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110915' AS STARTDATE,'9525454' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110915' AS STARTDATE,'11300000' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110915' AS STARTDATE,'14334444' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110915' AS STARTDATE,'14345757' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110919' AS STARTDATE,'12172020' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110919' AS STARTDATE,'13082020' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110920' AS STARTDATE,'6582929' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110923' AS STARTDATE,'13595050' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110927' AS STARTDATE,'17153636' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110928' AS STARTDATE,'9325757' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110929' AS STARTDATE,'17480404' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20110930' AS STARTDATE,'13290808' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111003' AS STARTDATE,'7243939' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111003' AS STARTDATE,'7410673' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111003' AS STARTDATE,'11191818' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111011' AS STARTDATE,'14302626' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111013' AS STARTDATE,'20042101' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111017' AS STARTDATE,'16352837' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111019' AS STARTDATE,'10573030' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111021' AS STARTDATE,'16181581' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111025' AS STARTDATE,'14332020' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111027' AS STARTDATE,'14404242' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111027' AS STARTDATE,'15301212' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111027' AS STARTDATE,'15303030' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111028' AS STARTDATE,'16514829' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111031' AS STARTDATE,'7401717' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111107' AS STARTDATE,'14444343' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111107' AS STARTDATE,'14444848' AS TIMEOFDAY,'3' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111107' AS STARTDATE,'14480101' AS TIMEOFDAY,'3' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111107' AS STARTDATE,'16311272' AS TIMEOFDAY,'4' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111108' AS STARTDATE,'13331919' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111111' AS STARTDATE,'9433080' AS TIMEOFDAY,'3' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111111' AS STARTDATE,'14540246' AS TIMEOFDAY,'4' AS Eventtype FROM DUAL UNION ALL
SELECT 'tbd123' AS TSKID,'20111114' AS STARTDATE,'7511220' AS TIMEOFDAY,'0' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20110509' AS STARTDATE,'10442063' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20110509' AS STARTDATE,'10502891' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111012' AS STARTDATE,'8333340' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111013' AS STARTDATE,'13122884' AS TIMEOFDAY,'7' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111013' AS STARTDATE,'13420329' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111014' AS STARTDATE,'13381646' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111018' AS STARTDATE,'12451804' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111019' AS STARTDATE,'14091263' AS TIMEOFDAY,'3' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111021' AS STARTDATE,'12355005' AS TIMEOFDAY,'4' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111021' AS STARTDATE,'12373030' AS TIMEOFDAY,'4' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111024' AS STARTDATE,'10203700' AS TIMEOFDAY,'8' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111101' AS STARTDATE,'12513479' AS TIMEOFDAY,'34' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111101' AS STARTDATE,'13440810' AS TIMEOFDAY,'35' AS Eventtype FROM DUAL UNION ALL
SELECT 'tvt003' AS TSKID,'20111107' AS STARTDATE,'14094345' AS TIMEOFDAY,'0' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101112' AS STARTDATE,'13061818' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101112' AS STARTDATE,'13173636' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101115' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101115' AS STARTDATE,'9215050' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101116' AS STARTDATE,'300101' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101117' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101118' AS STARTDATE,'300202' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101119' AS STARTDATE,'300404' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101119' AS STARTDATE,'11311616' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101119' AS STARTDATE,'15463839' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101122' AS STARTDATE,'300303' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101123' AS STARTDATE,'300101' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'300404' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15183838' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15302122' AS TIMEOFDAY,'32' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15312324' AS TIMEOFDAY,'32' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15313233' AS TIMEOFDAY,'11' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15314041' AS TIMEOFDAY,'10' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15315354' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101124' AS STARTDATE,'15320506' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101126' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101129' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101130' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101201' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101202' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101203' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101206' AS STARTDATE,'300303' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101206' AS STARTDATE,'10000808' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101206' AS STARTDATE,'10011718' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101206' AS STARTDATE,'10240203' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101207' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101208' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101216' AS STARTDATE,'10344243' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101216' AS STARTDATE,'10384748' AS TIMEOFDAY,'0' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101216' AS STARTDATE,'10391919' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101216' AS STARTDATE,'10392122' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101216' AS STARTDATE,'10420303' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101216' AS STARTDATE,'13041819' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'300303' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'8550707' AS TIMEOFDAY,'32' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'8552323' AS TIMEOFDAY,'11' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'8554141' AS TIMEOFDAY,'10' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'8555757' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'8561313' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101217' AS STARTDATE,'8583333' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101220' AS STARTDATE,'300202' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101220' AS STARTDATE,'8062021' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101221' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101222' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101223' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101227' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101228' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101229' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101230' AS STARTDATE,'300404' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20101231' AS STARTDATE,'300202' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110103' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110104' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110104' AS STARTDATE,'8573737' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110104' AS STARTDATE,'8574040' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110104' AS STARTDATE,'8583536' AS TIMEOFDAY,'23' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110105' AS STARTDATE,'300404' AS TIMEOFDAY,'23' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110106' AS STARTDATE,'300404' AS TIMEOFDAY,'23' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110106' AS STARTDATE,'9064040' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110107' AS STARTDATE,'300000' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110107' AS STARTDATE,'12255252' AS TIMEOFDAY,'2' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110107' AS STARTDATE,'12255455' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110110' AS STARTDATE,'300000' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110111' AS STARTDATE,'300101' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110111' AS STARTDATE,'14034343' AS TIMEOFDAY,'30' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110111' AS STARTDATE,'14265859' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110111' AS STARTDATE,'15265656' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110111' AS STARTDATE,'16203939' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110111' AS STARTDATE,'16322424' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION ALL
SELECT 'txs472' AS TSKID,'20110112' AS STARTDATE,'300404' AS TIMEOFDAY,'1' AS Eventtype FROM DUAL UNION)

OUTPUT:
TSKID   StartDate  EndDate   Eventtype
tbd123  20100928   20100930  2
tbd123  20100930   20101004  30
tbd123  20101020   20101122  2  and so on....same for all other tskids

2 个答案:

答案 0 :(得分:1)

这是你想要的球场吗?我没有oracle进行测试,所以我正在使用竞争对手:

with test (
...
)
select TSKID
     , min(STARTDATE) as min_startdate
     , max(STARTDATE) as max_startdate
     , Eventtype
     , grp
from (
    select TSKID, STARTDATE, Eventtype
        ,  row_number() over (partition by tskid order by STARTDATE)
         - row_number() over (partition by tskid, Eventtype order by STARTDATE) as grp
    from test
)
group by TSKID,Eventtype,grp
order by TSKID,Eventtype

答案 1 :(得分:1)

这是一个空白和岛屿问题。

with test as (...),
t as (
  select tskid, startdate, eventtype,
    dense_rank() over (partition by tskid order by startdate)
     - dense_rank() over (partition by tskid, eventtype order by startdate)
    as chain
  from test
)
select tskid, min(startdate) as startdate, max(startdate) as enddate,
  eventtype
from t
group by tskid, eventtype, chain
order by tskid, startdate;

TSKID  STARTDATE ENDDATE  EVENTTYPE
------ --------- -------- ---------
tbd123 20100928  20100930 2         
tbd123 20100930  20101004 30        
tbd123 20101020  20101122 2         
tbd123 20101122  20110608 30        
tbd123 20110620  20110623 2         
tbd123 20110623  20110624 1         
tbd123 20110623  20110912 30        
...

子查询(CTE)t使用比较两个tskid计算的技巧来识别每个eventtype / dense_rank组合的连续组。

然后,您可以获得每个链的最小值和最大值startdate

您也可以使用row_number(),但必须基于这些的不同值,因为多个timeofday字段会将其抛出;当我评论@Lennart的答案时,它会给你一个稍微不同的细分:

...
    row_number() over (partition by tskid order by startdate)
     - row_number() over (partition by tskid, eventtype order by startdate)
    as chain
  from (select distinct tskid, startdate, eventtype from test)
...

TSKID  STARTDATE ENDDATE  EVENTTYPE
------ --------- -------- ---------
tbd123 20100928  20100930 2         
tbd123 20100930  20101004 30        
tbd123 20101020  20101119 2         
tbd123 20101122  20101122 2         
tbd123 20101122  20101122 30        
tbd123 20101123  20110608 30        
tbd123 20110620  20110620 2         
tbd123 20110623  20110623 1         
tbd123 20110623  20110623 2         
tbd123 20110623  20110623 30        
...

不确定您想要的是什么,或者确实如果其中任何一个是正确的; dense_rank似乎与问题中的示例输出相匹配,但不确定这是否意味着什么。