我有2张桌子
Create table t (
employee varchar2(10),
job varchar2(5),
start_date date,
end_date date,
amt number
);
使用以下数据
insert into t values (1111,1,19-May-2008,20-May-2008,7);
insert into t values (1111,1,19-May-2010,19-May-2010,7);
insert into t values (1111,1,01-Jul-2010,01-Jul-2010,7);
insert into t values (1111,1,13-Feb-2012,13-Feb-2012,7);
insert into t values (1111,1,27-Nov-2013,27-Nov-2013,7);
insert into t values (1111,1,28-Nov-2013,28-Nov-2013,7);
insert into t values (1111,1,24-Aug-2014,24-Aug-2014,7);
insert into t values (1111,1,25-Aug-2014,25-Aug-2014,7);
insert into t values (1111,1,24-Aug-2015,25-Aug-2015,14);
insert into t values (1111,1,14-Nov-2015,14-Nov-2015,7);
insert into t values (1111,1,15-Nov-2015,16-Nov-2015,14);
insert into t values (1111,1,11-Sep-2016,11-Sep-2016,7);
insert into t values (1111,1,29-Sep-2016,29-Sep-2016,7);
insert into t values (1111,1,10-Nov-2016,10-Nov-2016,7);
insert into t values (1111,1,14-Nov-2016,15-Nov-2016,14);
Create table t1 (
employee varchar2(10),
job varchar2(5),
start_date date,
commence_date date,
);
使用以下数据
Insert into t values (1111, 1, 14-Nov-2007, 19-May-2007);
我正在尝试列出要从START_date开始按12个月分组显示的amt。结果应返回EMPLOYEE,JOB,PERIOD_START,PERIOD_END,AMT。如果一年内没有显示,则“AMT”列将显示“0”。结果应如下所示。
1111 1 19/05/2007 17/05/2008 0
1111 1 19/05/2008 18/05/2009 7
1111 1 19/05/2009 18/05/2010 0
1111 1 19/05/2010 18/05/2011 7
1111 1 19/05/2011 18/05/2012 14
1111 1 19/05/2012 18/05/2013 0
1111 1 19/05/2013 18/05/2014 14
1111 1 19/05/2014 18/05/2015 14
1111 1 19/05/2015 18/05/2016 35
1111 1 19/05/2016 18/05/2017 35
我试过这样做。但是我无法显示没有amt的行。示例预期结果中的第1行。任何帮助或建议将不胜感激。
以下是我正在使用的查询
SELECT DISTINCT
ss.EMPLOYEE,
ss.JOB,
ss.start_date commence,
ss.Year_frm_start Year,
SUM(ss.AMT) Over (Partition BY ss.EMPLOYEE, ss.JOB, ss.Year_frm_start) amt,
ss.start_date +numtoyminterval(ss.Year_frm_start,'Year') period_start,
(ss.start_date -1)+numtoyminterval(ss.Year_frm_start+1,'Year') period_end
FROM
(
SELECT
t.EMPLOYEE,
t.JOB,
t.AMT,
t1.start_date,
floor((t.start_DATE - t1.start_date) / 365) Year_frm_start
FROM
t,t1
WHERE
t.employee=t1.employee and t.job=t1.job
ORDER BY
t.start_date
)
ss
where ss.EMPLOYEE = '1111'
ORDER BY
ss.EMPLOYEE,
ss.Year_frm_start
;