我有簡單的表員工id,開始年份,結束年份。我必須找出有多少員工在最低年份工作。如果分年是2000年和最大。應該列出所有這些年份從2000年到2017年的工作僱員人數每年有多少員工工作?
回答
這是一種方法,但我相信有其他更好的方式可以向我們展示。
with MA_MITARBEITER_TH (user_id, grp_type, sap_valid_from, sap_valid_to) as
(
select 107, 'NOT_ACTIVE', to_date('23-09-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 107, 'NOT_ACTIVE', to_date('01-01-2012','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 107, 'NOT_ACTIVE', to_date('23-09-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 107, 'ACTIVE', to_date('01-07-2007','dd-mm-yyyy'), to_date('31-08-2007','dd-mm-yyyy') from dual union all
select 107, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('30-06-2007','dd-mm-yyyy') from dual union all
select 107, 'NOT_ACTIVE', to_date('01-01-2012','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 107, 'ACTIVE', to_date('01-09-2007','dd-mm-yyyy'), to_date('31-12-2011','dd-mm-yyyy') from dual union all
select 107, 'ACTIVE', to_date('01-04-2006','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 107, 'ACTIVE', to_date('01-09-2007','dd-mm-yyyy'), to_date('22-09-2011','dd-mm-yyyy') from dual union all
select 106, 'NOT_ACTIVE', to_date('01-02-2007','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 106, 'NOT_ACTIVE', to_date('01-02-2007','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 106, 'ACTIVE', to_date('15-05-2006','dd-mm-yyyy'), to_date('31-01-2007','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-01-2013','dd-mm-yyyy'), to_date('31-05-2013','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('31-10-2013','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('31-12-2008','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-01-2009','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-01-2014','dd-mm-yyyy'), to_date('01-12-2014','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-11-2016','dd-mm-yyyy'), to_date('31-12-2016','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('31-10-2016','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-03-2017','dd-mm-yyyy'), to_date('31-05-2017','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-01-2017','dd-mm-yyyy'), to_date('28-02-2017','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-06-2017','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('21-09-2006','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2011','dd-mm-yyyy'), to_date('31-12-2012','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-03-2010','dd-mm-yyyy'), to_date('31-12-2012','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2012','dd-mm-yyyy'), to_date('31-12-2012','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-11-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-11-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-11-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-07-2009','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('31-05-2008','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-08-2009','dd-mm-yyyy'), to_date('28-02-2010','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-11-2016','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('31-10-2016','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2017','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2014','dd-mm-yyyy'), to_date('31-10-2016','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('01-01-2014','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 105, 'ACTIVE', to_date('01-03-2010','dd-mm-yyyy'), to_date('31-12-2012','dd-mm-yyyy') from dual union all
select 105, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('31-10-2016','dd-mm-yyyy') from dual union all
select 104, 'NOT_ACTIVE', to_date('01-09-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-01-2009','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('31-12-2008','dd-mm-yyyy') from dual union all
select 104, 'NOT_ACTIVE', to_date('01-09-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-10-2010','dd-mm-yyyy'), to_date('31-08-2011','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-12-2009','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('31-05-2008','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-01-2010','dd-mm-yyyy'), to_date('30-09-2010','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 104, 'ACTIVE', to_date('10-04-2006','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('20-08-2007','dd-mm-yyyy'), to_date('31-05-2008','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-08-2006','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('31-12-2008','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('30-06-2007','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-08-2007','dd-mm-yyyy'), to_date('19-08-2007','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-01-2009','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-07-2007','dd-mm-yyyy'), to_date('31-07-2007','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-05-2013','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('30-09-2013','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-11-2009','dd-mm-yyyy'), to_date('31-05-2013','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('31-12-2014','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-10-2009','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-07-2007','dd-mm-yyyy'), to_date('19-08-2007','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('04-12-2013','dd-mm-yyyy'), to_date('31-12-2014','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('02-12-2016','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('01-01-2017','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'ACTIVE', to_date('03-12-2016','dd-mm-yyyy'), to_date('31-12-2016','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-11-2009','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2012','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-05-2013','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('30-09-2013','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('31-12-2014','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('01-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('02-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('03-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('02-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('04-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('03-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('05-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('04-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('06-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('05-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('06-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('07-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('07-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('08-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('09-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('08-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('09-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('10-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('11-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('10-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('12-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('11-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('13-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('12-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('14-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('13-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('15-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('14-01-2015','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-10-2013','dd-mm-yyyy'), to_date('31-12-2014','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('03-12-2016','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('02-12-2016','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('03-12-2016','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 100, 'NOT_ACTIVE', to_date('04-12-2013','dd-mm-yyyy'), to_date('02-12-2016','dd-mm-yyyy') from dual union all
select 103, 'NOT_ACTIVE', to_date('01-12-2007','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 103, 'ACTIVE', to_date('23-05-2006','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 103, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('30-11-2007','dd-mm-yyyy') from dual union all
select 103, 'NOT_ACTIVE', to_date('01-12-2007','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-12-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-12-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-12-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-12-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-08-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-01-2012','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-08-2011','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-08-2011','dd-mm-yyyy'), to_date('31-05-2013','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-06-2013','dd-mm-yyyy'), to_date('30-11-2013','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('02-11-2008','dd-mm-yyyy'), to_date('31-12-2008','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-01-2009','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-11-2008','dd-mm-yyyy'), to_date('31-12-2008','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-01-2015','dd-mm-yyyy'), to_date('13-12-2016','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('14-12-2016','dd-mm-yyyy'), to_date('31-12-2016','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-01-2017','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-05-2007','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-06-2010','dd-mm-yyyy'), to_date('30-09-2010','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-12-2007','dd-mm-yyyy'), to_date('27-05-2008','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-11-2008','dd-mm-yyyy'), to_date('01-11-2008','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('30-06-2008','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-07-2008','dd-mm-yyyy'), to_date('31-10-2008','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('31-10-2007','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-05-2010','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-11-2007','dd-mm-yyyy'), to_date('30-11-2007','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('02-11-2008','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-10-2010','dd-mm-yyyy'), to_date('31-07-2011','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('28-05-2008','dd-mm-yyyy'), to_date('31-05-2008','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('02-12-2014','dd-mm-yyyy'), to_date('13-12-2016','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('14-12-2016','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 102, 'ACTIVE', to_date('01-07-2006','dd-mm-yyyy'), to_date('30-04-2007','dd-mm-yyyy') from dual union all
select 102, 'NOT_ACTIVE', to_date('01-01-2017','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 101, 'NOT_ACTIVE', to_date('01-09-2010','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-04-2009','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-01-2009','dd-mm-yyyy'), to_date('31-03-2009','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-01-2009','dd-mm-yyyy'), to_date('28-02-2009','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('31-12-2008','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-03-2009','dd-mm-yyyy'), to_date('30-06-2009','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-07-2007','dd-mm-yyyy'), to_date('29-02-2008','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('27-05-2008','dd-mm-yyyy'), to_date('31-05-2008','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('27-03-2008','dd-mm-yyyy'), to_date('26-05-2008','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-04-2007','dd-mm-yyyy'), to_date('31-05-2007','dd-mm-yyyy') from dual union all
select 101, 'NOT_ACTIVE', to_date('01-09-2010','dd-mm-yyyy'), to_date('31-12-9999','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-03-2008','dd-mm-yyyy'), to_date('26-03-2008','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-06-2008','dd-mm-yyyy'), to_date('28-02-2009','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('23-10-1992','dd-mm-yyyy'), to_date('31-03-2007','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-06-2007','dd-mm-yyyy'), to_date('30-06-2007','dd-mm-yyyy') from dual union all
select 101, 'ACTIVE', to_date('01-07-2009','dd-mm-yyyy'), to_date('31-08-2010','dd-mm-yyyy') from dual
)
--select * from MA_MITARBEITER_TH;
,
-- uncomment the below line to point to your db
-- ;with
emp (id, hired, fired) as
(
select user_id, to_number(to_char(sap_valid_from, 'yyyy')), to_number(to_char(sap_valid_to,'yyyy'))
from
(
select user_id, min(sap_valid_from) sap_valid_from, max(sap_valid_to) sap_valid_to
from MA_MITARBEITER_TH
where grp_type = 'ACTIVE'
group by user_id
)
)
--select * from emp;
,
-- grouping of hired by year
hy (year, cnt) as
(
select hired, count(hired)
from emp
group by hired
)
,
-- grouping of fired by year
fy (year, cnt) as
(
select fired, count(fired)
from emp
group by fired
)
,
-- put them together with a cte
-- it takes care of years where no HR activity
-- anchor is set to the min hired year.
cte (year, hired, fired, emp_cnt) as
(
select hy.year, nvl(hy.cnt ,0), nvl(fy.cnt,0), hy.cnt - nvl(fy.cnt,0)
from hy
left outer join
fy
on hy.year = fy.year
where hy.year = (select min(year) from hy)
union all
select cte.year+1, nvl(hy.cnt,0), nvl(fy.cnt,0), cte.emp_cnt + nvl(hy.cnt,0) - nvl(fy.cnt,0)
from cte
left outer join
hy
on (cte.year+1 = hy.year)
left outer join
fy
on (cte.year+1 = fy.year)
where cte.year < to_char(sysdate, 'yyyy')
)
select * from cte;
;
我正在考慮更多的動態SQL。請參閱上面的示例表和註釋。除此之外,我非常感謝您的幫助! – HankerPL
NOT_ACTIVE暗示什麼?它是否會影響報告查詢?有許多重複的行像第2行和第4行。如果相同的USER_ID在特定年份中有多個條目,那麼您是否將它們作爲單獨的或相同的添加? – Slkrasnodar
在這種情況下,我們只需要爲特定的user_id選擇ACTIVE GROUP條目,並從VALID_FROM和max中選擇最小值。 VALID_TO的值來檢查某個員工在公司工作的時間(時間範圍)。除此之外,我們通過從DATE中提取年份(VALID_FROM,VALID_TO&SYSDATE作爲實際年份=在這種情況下,我假定提取(年份從SYSDATE)應該被處理爲最大年份2017 =最大值而不是9999) MAX;) – HankerPL
我結合一些疑問,我想現在是我的習慣,請確認是否是此問題少的解決方案:
with YEARS as ( select (to_char(SAP_VALID_FROM,'YYYY') + level -1) AS YEARS from ( SELECT MIN(SAP_VALIDFROM) AS SAP_VALID_FROM, MAX(SYSDATE) AS SAP_VALID_TO FROM MA_MITARBEITER_TH ) YEARS connect by level < = to_char(SYSDATE,'YYYY') - to_char(SAP_VALID_FROM,'YYYY')+1 ), YEARS_SCOPE AS ( SELECT PERSONALNUMMER, EXTRACT(YEAR FROM (MIN(SAP_VALIDFROM))) AS ARBEIT_BEGINN, CASE WHEN EXTRACT(YEAR FROM (MAX(SAP_VALIDTO)))='9999' THEN EXTRACT(YEAR FROM (MAX(SYSDATE))) ELSE EXTRACT(YEAR FROM (MAX(SAP_VALIDTO))) END AS ARBEIT_SCHLUSS FROM MA_MITARBEITER_TH WHERE TIMEGROUP='AKT' GROUP BY PERSONALNUMMER ) select COUNT(*), YEARS FROM YEARS,YEARS_SCOPE WHERE YEARS BETWEEN ARBEIT_BEGINN AND ARBEIT_SCHLUSS GROUP BY YEARS ORDER BY YEARS;
- 1. SQL - 有多少員工數量的工作每個項目
- 2. 每位員工有多少名經理?
- 3. Scala/AKKA - 有多少演員在「工作」?
- 4. 員工並檢查每位員工的黃金年齡
- 5. 工作人員和教職員工姓名和年薪降序
- 6. 員工每週工作時間
- 7. SQL有多少僱員的工作,集團按小時
- 8. php strtotime和工作多年?
- 9. 統計名稱一次(每年),不管有多少年有
- 10. 每個部門有多少僱主工作?
- 11. 如何確定Excel工作簿中有多少個工作表?
- 12. SQL計算每位員工每週工作的班次
- 13. 顯示員工每
- 14. 爲每天10M +的工作隊列調整工作人員
- 15. 我可以使用多少名後臺工作人員?
- 16. Rails&Heroku:我需要多少個工作人員/ dynos
- 17. 查找每年具有相同生日日期的所有員工對
- 18. 如何通過一年獲得有關集團總員工的員工數
- 19. 如果工作超過一年,則更新員工工資2美元
- 20. 有多少企業禁止員工使用Flash插件?
- 21. Akka:每個演員系統有多少個調度員?
- 22. SQL:每週工作日訪問多少次?
- 23. 宏在2007年刪除行不工作在2003年工作
- 24. 顯示員工姓名,部門名稱,薪水,年級,經驗(EX:XX年YY月ZZ天)SQL的所有員工
- 25. 查找連續工作日期爲每個員工
- 26. 刪除每第n個員工不工作
- 27. 設置每位員工的工作百分比
- 28. 員工周每小時
- 29. 計算爲每個員工
- 30. 工作人員在Kue中處理多種工作類型
能否請您包括樣本表或您的模式,以便我們不必猜測它看起來像什麼? – Manngo
那麼你想要2000年的一行和那一年的員工數量,一年的2001年,當年的員工數量,等等?你有一個包含所有年份數字的表格嗎? –
[鏈接到示例表](https://drive.google.com/file/d/0B2z2BF4fbWh8aE1tMldQNG5CVDA/view?usp=sharing) – HankerPL