6
Oracle SQL新手在這裏和第一次張貼海報。合併日期範圍
我認爲這將是簡單的,直到我意識到我無法弄清楚如何分割返回分配。
這裏是我的作業表:ASGN
ID ST_DT END_DT POS LOCN STATUS WAGE_CD
-- ---------- ---------- ----- ---- ------ -------
A 12-31-2006 08-16-2009 CLERK LAX 3 A
A 08-17-2009 10-04-2009 CLERK LAX 0 Z
A 10-05-2009 06-30-2010 OPR NYC 3 A
A 07-01-2010 12-31-2010 OPR NYC 3 B
A 01-01-2011 06-30-2012 OPR NYC 3 C
A 07-01-2012 04-09-2013 OPR NYC 3 D
A 04-10-2013 06-30-2013 CLERK LAX 3 A
A 07-01-2013 08-10-2014 CLERK LAX 3 B
A 07-01-2013 08-10-2014 CLERK LAX 3 C
B 04-10-2013 05-31-2013 SUP LAX 3 A
B 06-01-2013 06-30-2014 SUP LAX 0 Z
B 07-01-2013 08-10-2014 SUP LAX 3 B
B 08-11-2014 08-11-2014 CLERK NYC 3 A
B 08-12-2014 01-11-2015 SUP LAX 3 A
B 01-12-2015 02-10-2016 SUP LAX 3 B
B 02-11-2016 08-12-2016 OPER SFO 3 A
B 02-11-2016 08-12-2016 OPER SFO 3 B
我已經有意想不到的效果試過下面這樣。
SELECT *
FROM (
SELECT ID
,MIN(ST_DT) ST_DT
,MAX(END_DT) END_DT
,POS
,LOCN
,STATUS
FROM ASGN
GROUP BY ID, LOCN, POS, STATUS
) SUBQRY
ORDER BY ID, ST_DT
意外的結果,但有道理。這是返回分配不與以前的分配相結合的地方。
ID ST_DT END_DT POS LOCN STATUS
-- ---------- ---------- ----- ---- ------
A 12-31-2006 08-10-2014 CLERK LAX 3
A 08-17-2009 10-04-2009 CLERK LAX 0
A 10-05-2009 04-09-2010 OPR NYC 3
B 04-10-2013 02-10-2015 SUP LAX 3
B 06-01-2013 06-30-2014 SUP LAX 0
B 08-11-2014 08-11-2014 CLERK NYC 3
B 02-11-2016 08-12-2016 OPER SFO 3
而且看到我想結果在相鄰日期每ID,位置,位置相結合,STATUS:
ID ST_DT END_DT POS LOCN STATUS
-- ---------- ---------- ----- ---- ------
A 12-31-2006 08-16-2009 CLERK LAX 3
A 08-17-2009 10-04-2009 CLERK LAX 0
A 10-05-2009 04-09-2010 OPR NYC 3
A 04-10-2013 08-10-2014 CLERK LAX 3
B 04-10-2013 05-31-2013 SUP LAX 3
B 06-01-2013 06-30-2014 SUP LAX 0
B 07-01-2013 08-10-2014 SUP LAX 3
B 08-11-2014 08-11-2014 CLERK NYC 3
B 08-12-2014 02-10-2015 SUP LAX 3
B 02-11-2016 08-12-2016 OPER SFO 3
我問了一個多賽季的Oracle SQL程序員,他說我我們需要PLSQL,但我認爲必須有一種方法可以通過SQL來完成這項工作。
測試設置腳本:
create table asgn
(id varchar2(10)
,st_dt date
,end_dt date
,pos varchar2(10)
,locn varchar2(10)
,status number
,wage_cd varchar2(10));
insert into asgn values('A',to_date('12-31-2006','mm-dd-yyyy'),to_date('08-16-2009','mm-dd-yyyy'),'CLERK','LAX',3,'A');
insert into asgn values('A',to_date('08-17-2009','mm-dd-yyyy'),to_date('10-04-2009','mm-dd-yyyy'),'CLERK','LAX',0,'Z');
insert into asgn values('A',to_date('10-05-2009','mm-dd-yyyy'),to_date('06-30-2010','mm-dd-yyyy'),'OPR','NYC',3,'A');
insert into asgn values('A',to_date('07-01-2010','mm-dd-yyyy'),to_date('12-31-2010','mm-dd-yyyy'),'OPR','NYC',3,'B');
insert into asgn values('A',to_date('01-01-2011','mm-dd-yyyy'),to_date('06-30-2012','mm-dd-yyyy'),'OPR','NYC',3,'C');
insert into asgn values('A',to_date('07-01-2012','mm-dd-yyyy'),to_date('04-09-2013','mm-dd-yyyy'),'OPR','NYC',3,'D');
insert into asgn values('A',to_date('04-10-2013','mm-dd-yyyy'),to_date('06-30-2013','mm-dd-yyyy'),'CLERK','LAX',3,'A');
insert into asgn values('A',to_date('07-01-2013','mm-dd-yyyy'),to_date('08-10-2014','mm-dd-yyyy'),'CLERK','LAX',3,'B');
insert into asgn values('A',to_date('07-01-2013','mm-dd-yyyy'),to_date('08-10-2014','mm-dd-yyyy'),'CLERK','LAX',3,'C');
insert into asgn values('B',to_date('04-10-2013','mm-dd-yyyy'),to_date('05-31-2013','mm-dd-yyyy'),'SUP','LAX',3,'A');
insert into asgn values('B',to_date('06-01-2013','mm-dd-yyyy'),to_date('06-30-2014','mm-dd-yyyy'),'SUP','LAX',0,'Z');
insert into asgn values('B',to_date('07-01-2013','mm-dd-yyyy'),to_date('08-10-2014','mm-dd-yyyy'),'SUP','LAX',3,'B');
insert into asgn values('B',to_date('08-11-2014','mm-dd-yyyy'),to_date('08-11-2014','mm-dd-yyyy'),'CLERK','NYC',3,'A');
insert into asgn values('B',to_date('08-12-2014','mm-dd-yyyy'),to_date('01-11-2015','mm-dd-yyyy'),'SUP','LAX',3,'A');
insert into asgn values('B',to_date('01-12-2015','mm-dd-yyyy'),to_date('02-10-2016','mm-dd-yyyy'),'SUP','LAX',3,'B');
insert into asgn values('B',to_date('02-11-2016','mm-dd-yyyy'),to_date('08-12-2016','mm-dd-yyyy'),'OPER','SFO',3,'A');
insert into asgn values('B',to_date('02-11-2016','mm-dd-yyyy'),to_date('08-12-2016','mm-dd-yyyy'),'OPER','SFO',3,'B');
這太好了!謝謝! 對於兩行的員工A,我想表明兩者之間的WAGE_CD存在差異。我可以在表中添加另一個級別的信息,但不想進一步混淆。 現在,我只需要弄清楚我收到的ORA-01841:(full)year ...錯誤。 – krwoibnvts